"Anthony Lansbergen" <an*****@EdAsys.nl> wrote in message
news:3f***********************@news.xs4all.nl...
Hi,
I want to create an abstract base class for singletons.
The base class should demand that the derived classes implement a static
instance member operation.
Is it possible to create a pure virtual static member function in a base
classe?
If not, is there an better elegant solution for this problem?
I'd recommend to use a templated wrapper for your singletons. Look at the
approaches in Modern Design C++ or you can use the following simple
solution:
template<class T, class Tag = void>
class CSingleton
{
private:
CSingleton(void) { }
~CSingleton(void) { }
CSingleton( const CSingleton& rhs );
CSingleton& operator=( const CSingleton& rhs);
public:
static T& Instance() {
static T m_Instance;
return m_Instance;
}
};
An example class which should become a "singleton" would for example look
like this:
class CObj {
protected:
CObj() {};
CObj& operator=( const CObj& rhs);
CObj( const CObj& rhs );
public:
~CObj() {};
friend class CSingleton<CObj>;
int m_i;
};
The usage would be like this:
CObj& MyObj = CSingleton<CObj>::Instance();
HTH
Chris