singleton base class

  • Thread starter Anthony Lansbergen
  • Start date
A

Anthony Lansbergen

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?

Thanks in advance,
Anthony Lansbergen
 
C

Chris Whitworth

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?

You can't have a virtual static member. The member will be static to the
base class, always. This caused me quite a few headaches until I realised
what was going on; in the end, I had to use templates to get round it.

Chris
--
Chris Whitworth * On XBL as parm * Playing Ghost Recon, MechAssault, MotoGP

"back when I was young, we had to travel back in time to put the tape
in so the game would load before we died."
--otama, ua2
 
V

Victor Bazarov

Anthony Lansbergen said:
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?

No. "virtual" and "static" contradict each other.
If not, is there an better elegant solution for this problem?

Think templates. If you define the entire class or some member
function to use the "instance" member function of the template
argument, then it will work as if you had that requirement for
a "virtual static".

template<class Derived> class MySingleton {
...
Derived::instance()
...
};

And the user of your template would do

class MySpecialSingleton : MySingleton<MySpecialSingleton> {
...
};

Victor
 
C

Chris Theis

Anthony Lansbergen said:
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
 
E

Evan

Chris Theis said:
Anthony Lansbergen said:
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++ [snip]

Or download said approaches from
http://www.awl.com/cseng/titles/0-201-70431-5, though they very well
may not make total sense without reading the book first. I can't
remember if it uses template partial specalization, but if so, you'll
also neeed a really recent compiler to use.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top