Derivable singleton implementation
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.