Discussion:
Derivable singleton implementation
(too old to reply)
Faisal
2009-12-28 13:15:11 UTC
Permalink
I have a generic Singleton class.

//Singleton.h

template <class TYPE>
class CSingleTon
{
protected:
CSingleTon()
{}

virtual ~CSingleTon(){}

public:
static TYPE* Instance()
{
if( NULL == m_pInstance )
m_pInstance = new TYPE();

return m_pInstance;
}

static void Destroy()
{
delete m_pInstance;
m_pInstance = NULL;
}

protected:
static TYPE* m_pInstance;
};

template <class TYPE>
TYPE* CSingleTon<TYPE>::m_pInstance = NULL;

#define SET_SINGLETON( classname ) friend class CSingleTon<classname>;

//end of file Singleton.h


And I uses this class to create singletone classes
like

class CGlobalDataStore : public CSingleTon<CGlobalDataStore>
{
SET_SINGLETON(CGlobalDataStore);
};

This is working correctly.

Now I want to derive a class from CGlobalDataStore( which is also
singleton).
I tried

class CGlobalSignalStore : public CGlobalDataStore
{
SET_SINGLETON(CGlobalSignalStore);
};


But compilation fails showing error.
error C2440: 'initializing' : cannot convert from 'CGlobalDataStore *'
to 'CGlobalSignalStore *'
1> Cast from base to derived requires dynamic_cast or
static_cast


int main(int argc, char *argv[])
{
//This line working correctly
CGlobalDataStore* pGlobalDataStore = CGlobalDataStore::Instance();
//This shows the error
CGlobalSignalStore* pGlobalSignalStore = CGlobalSignalStore::Instance
();

return 0;
}

So my question is, How can I implement a derivable singleton class.
Igor Tandetnik
2009-12-28 15:23:52 UTC
Permalink
Post by Faisal
I have a generic Singleton class.
//Singleton.h
template <class TYPE>
class CSingleTon
{
CSingleTon()
{}
virtual ~CSingleTon(){}
static TYPE* Instance()
{
if( NULL == m_pInstance )
m_pInstance = new TYPE();
return m_pInstance;
}
static void Destroy()
{
delete m_pInstance;
m_pInstance = NULL;
}
static TYPE* m_pInstance;
};
template <class TYPE>
TYPE* CSingleTon<TYPE>::m_pInstance = NULL;
#define SET_SINGLETON( classname ) friend class CSingleTon<classname>;
//end of file Singleton.h
And I uses this class to create singletone classes
like
class CGlobalDataStore : public CSingleTon<CGlobalDataStore>
{
SET_SINGLETON(CGlobalDataStore);
};
This is working correctly.
Now I want to derive a class from CGlobalDataStore( which is also
singleton).
Don't derive from CSingleTon in the first place. You can do something like this:

class CGlobalDataStore {
static CGlobalDataStore* Instance() {
return CSingleTon<CGlobalDataStore>::Instance();
};

class CAnotherStore: public CGlobalDataStore {
static CAnotherStore* Instance() {
return CSingleTon<CAnotherStore>::Instance();
};


I don't quite see how this design makes sense though. You now have two singletons - one CGlobalDataStore and one CAnotherStore - so there are two instances of CGlobalDataStore in the program (one on its own and one as a subobject of CAnotherStore), which kind of defeats the point of it being a singleton.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Leigh Johnston
2009-12-28 16:54:59 UTC
Permalink
Try using the Meyers Singleton instead (no need for a Destroy method then):

template <typename T>
struct singleton
{
static T& instance()
{
static T sInstance;
return sInstance;
}
};

struct foo : singleton<foo>
{
foo() { std::cout << "foo::foo()\n"; }
~foo() { std::cout << "foo::~foo()\n"; }
void func() { std::cout << "foo::func()\n"; }
};

int main()
{
foo::instance().func();
}

However be aware that singletons should be used rarely not routinely, take a
look at http://i42.co.uk/stuff/spaghetti.htm

Loading...