By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,871 Members | 2,464 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,871 IT Pros & Developers. It's quick & easy.

lightweight singleton template using references

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Sean Dettrick wrote:
Hi,
apologies for yet another Singleton posting.

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

// first instance:

myvec V = Singleton<myvec>::Instance();
You probably meant this:

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

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.

Jul 19 '05 #2

P: n/a
Gianni Mariani <gi*******@mariani.ws> wrote in message news:<bk********@dispatch.concentric.net>...
Sean Dettrick wrote:
Hi,
apologies for yet another Singleton posting.


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

// first instance:

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


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();}

};
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.