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