A Singleton Base and derived class I have a base and derived class since I need to have different kinds of concrete classes.
I cannot use the getInstance() function in the base class since I cannot make it both static and virtual.




class ITrafficEventsReturner {

    public:    virtual ~ITrafficEventsReturner() {} ;

    protected:

       ITrafficEventsReturner() { };

       // no definition for copy constructor and assignment operator  
       ITrafficEventsReturner(const ITrafficEventsReturner&amp;);      
       ITrafficEventsReturner&amp; operator=(const ITrafficEventsReturner&amp;); };


    class ConcreteTrafficReturner : public ITrafficEventsReturner {

    public:
        static ConcreteTrafficReturner* getInstance() {
          if (myself == 0)
          {
             myself = new ConcreteTrafficReturner();
          }

          return myself;    }

       ~ConcreteTrafficReturner()
        {
          if (myself)
          {
             delete myself;
             myself = 0;
          }
        }

    private :    ConcreteTrafficReturner() : ITrafficEventsReturner()    {

       };

       static ConcreteTrafficReturner* myself; };

    ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;

    int main() 
    {
       ITrafficEventsReturner* ConcreteTrafficReturner =     
            ConcreteTrafficReturner::getInstance();

       delete ConcreteTrafficReturner;
    };


This seems to be the possible solution :



class SingletonInterface
{
public:
   template &lt;class T&gt;
   static T&amp; getInstance() 
   {
      return T::getInstance();
   }

private:
   SingletonInterface() { };
};

class TrafficGenerator 
{
public:

   static TrafficGenerator&amp; getInstance()
   {
      static TrafficGenerator obj;
      return obj;
   }

   void whatAmI()
   {
      std::cout &lt;&lt; " I am Traffic Generator" &lt;&lt; std::endl;
   }

private:

   TrafficGenerator () {};
};

int main() 
{

   TrafficGenerator&amp; hTG = SingletonInterface::getInstance&lt;TrafficGenerator&gt;() ;
   hTG.whatAmI();
};
Please never do this again:

    static ConcreteTrafficReturner* getInstance() {
      if (myself == 0)
      {
         myself = new ConcreteTrafficReturner();
      }


Problem 1:

You are not providing a concept of ownership. The user of your class has to actually read the code to understand that you are not only returning the pointer but ownership of the pointer.

Problem 2:

You are returning a pointer. So who decides when it should be deleted. What happens if the user of your class forgets to call delete!

A better way to implement this is:

    static ConcreteTrafficReturner&amp; getInstance()
    {
       static ConcreteTrafficReturner    instance;    // created the first time
                                                      // get instance is called.
                                                      //
                                                      // automatically destroyed
                                                      // with other static storage
                                                      // duration objects.

        return instance;
    }


But really the above is a text book example (not a real world example) of a singelton. The singelton pattern is actually dangerous in that it binds your implementation to an object with mutable state. As such it should not be used in isolation it should be used in conjunction with a creator pattern (A factory is usually a good choice).

Note: Singelton is a design pattern. But in a lot of places it is also considered and anti-pattern so be very careful with its usage.

Template problem

Though technically correct. You may find problems with this design when using shared libraries. As each shared library may have its own instantiation of the templated object that is not shared. Thus you actually have multiple instances of your singelton.

Summary:

Personally I would not use a singelton (I think you can use other techniques). But if you have to use a singelton then it would look like this:

class MySingInterface
{
    public:
       virtual ~MySingInterface() {}  // Never foget the virtual destructor of
                                      // a class with virtual functions.
       // Other interfaces
};
class MySingFactory
{   
    public:
        virtual ~MySingInterfaceFactory() {}
        virtual MySingInterface&amp; createMySingInterface() = 0;
};

class MySingBuilder
{
    static std::vector&lt;MySingFactory*&gt;   factories;             // This could be a map
                                                                // of factories or 
                                                                // anything else that
                                                                // makes sense to your
                                                                // project.


       static MySingInterface&amp; buildInstance()
       {
             // decide what factory to use.
             // then call build (EG if you are in debug mode or unit test mode
             // you may want to build a different type of object from normal).
             //
             // I have shown an example where you can register multiple
             // factories but that may be overkill for you. Maybe you only need
             // to register one factory (which you decide in main and depends
             // on input arguments or config options or environment variables etc...)

             return factories[someIndex]-&gt;createMySingInterface();
       }
    public:
       MySingInterface&amp;  getInstance()
       {
           static MySingInterface&amp; instance = buildInstance();
           return instance;
       }

       static void addFactory(MySingFactory&amp; factory)
       {
           factories.push_back(&amp;factory);
       }
};

MySingFactoryForTest   mySingFactoryForTest;
MySingFactoryForNormal mySingFactoryForNormal;

int main()
{
    if (getenv("UNIT_TEST") != nullptr)
    {
        MySingBuilder::addFactory(mySingFactoryForTest);
    }
    else
    {
        MySingBuilder::addFactory(mySingFactoryForNormal);
    }

    MySingInterface&amp; mSI = MySingInterfaceBuilder::getInstance();
}