title: Singleton and Abstract base class in C++
body: <p>Recently I got question on implement Singleton but abstract base class involved.
  Suppose we have class hierarchy like this:</p>
  
  <pre><code>class IFoo {...}; // it's ABC
  class Foo : public IFoo {...};
  </code></pre>
  
  <p>we have singleton class defined as follows:</p>
  
  <pre><code>template &lt;typename T&gt;
  class Singleton
  {
  public:
  static T* Instance() {
     if (m_instance == NULL) {
        m_instance = new T();
     }
     return m_instance;
  }
  private:
  static T* m_instance;
  };
  </code></pre>
  
  <p>So if I want to use like following: <code>IFoo::Instance()-&gt;foo();</code> what should I do?</p>
  
  <p>If I do this: <code>class IFoo : public Singleton&lt;IFoo&gt; {...};</code> it won't work since Singleton will call IFoo's ctor but IFoo is a ABC so can not be created.</p>
  
  <p>And this: <code>class Foo : public IFoo, public Singleton&lt;Foo&gt; {...};</code> can't work too, because this way class IFoo doesn't have the interface for method Instance(), so the call <code>IFoo::Instance()</code> will fail.</p>
  
  <p>Any ideas?</p>