lightweight singleton template using references

S

Sean Dettrick

Hi,
apologies for yet another Singleton posting.

From reading around previous postings etc, I've cobbled together two
Singleton template definitions - one that returns a reference, and one
that returns a pointer. I understand the pointer one and it works,
but the reference one doesn't work as I expect. Can anybody explain
this to me? (Code below).

I'm not really looking for thread safety or for something as general
as in Loki. Really am just trying to figure out what's going on.

The error is that two subsequent calls to
Singleton<myType>::Instance() seem to return references to two
different objects, though confusingly, the myType constructor is
called only once (and the myType destructor called 3 times, and the
Singleton destructor not at all):

// CODE OUTPUT:

unix> ./a.out
Singleton<T>::Instance():
myvec constructor:
10 bffff610 <--- first singleton
Singleton<T>::Instance():
0 bffff648 <--- second singleton - different size and
address!
myvec destructor:
myvec destructor:
myvec destructor:
unix>

// COMPLETE SOURCE CODE:
// attempt to have a Singleton template which returns
// a reference.
#include <iostream>
#include <vector>
using namespace std;

//----------------------------------
// Singleton template that returns a reference:

template< class T >
class Singleton : public T
{
private:
Singleton(){cout << "Singleton constructor:";}
Singleton( const Singleton& );
~Singleton(){ cout << "Singleton destructor:";}
public:
static T& Instance(){
cout << "Singleton<T>::Instance():"<<endl;
static T t;
return t;
}
};

//----------------------------------
// just a dumb class to use as the template class T
// in the Singleton<T>:
class myvec
{
private:
int len;

public:
vector<int> v;

myvec(){ cout << "myvec constructor: "<<endl;}
~myvec(){ cout << "myvec destructor: " << endl;}
myvec( const myvec& rhs ): v(rhs.v), len(rhs.len) {}
myvec& operator=(const myvec& rhs){ v=rhs.v; len=rhs.len; return
*this; }

void init( int N ){ v.resize(N); len=N; }
void resize( int N ){ v.resize(N); len=N; }
int size(){ return v.size();}

};


//----------------------------------
// Main:
int main(){

// first instance:

myvec V = Singleton<myvec>::Instance();
V.init( 10 );

cout << V.size() << " " << &V << endl;

// second instance:

myvec W = Singleton<myvec>::Instance();

cout << W.size() << " " << &W << endl;

// NB W.size()=0 while V.size() = 10!
// also W and V have different addresses!
}

Thanks for any comments,
Sean Dettrick
 
G

Gianni Mariani

Sean said:
Hi,
apologies for yet another Singleton posting.
//----------------------------------
// Main:
int main(){

// first instance:

myvec V = Singleton<myvec>::Instance();

You probably meant this:

V.init( 10 );

cout << V.size() << " " << &V << endl;

// second instance:

myvec W = Singleton<myvec>::Instance();

myvec & W = Singleton<myvec>::Instance();


myvec V declares a myvec V.

I don't think this creates the singleton you were thinking.
 
S

Sean Dettrick

Gianni Mariani said:
You probably meant this:

myvec & V = Singleton<myvec>::Instance();


Thank you! That solved it.

And now if I make the myvec constructors and destructors private, and
declare Singleton<myvec> as its friend, then I seem to be protected
against the creation of any other myvecs (assuming single threading).
Any other comments?

class myvec
{
friend class Singleton<myvec>;
private:
int len;

myvec(){ cout << "myvec constructor: "<<endl;}
~myvec(){ cout << "myvec destructor: " << endl;}
myvec( const myvec& rhs ): v(rhs.v), len(rhs.len) {}
myvec& operator=(const myvec& rhs){ v=rhs.v; len=rhs.len; return
*this; }
public:
vector<int> v;

void init( int N ){ v.resize(N); len=N; }
void resize( int N ){ v.resize(N); len=N; }
int size(){ return v.size();}

};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top