title: Multiple Singleton Instances
body: <p>I am writing a library of utility classes, many of which are singletons.  I have implemented them as such using inheritance:</p>
  
  <pre><code>template &lt;class T&gt;
  class Singleton {
      public:
          T&amp; getInstance() {
              if(m_instance == 0) {
                   m_instance = new T;
              }
  
              return m_instance;
          }
      private:
          static T* m_instance;
  };
  
  class SomeClass : public Singleton&lt;SomeClass&gt; {
      public:
          SomeClass() {}
          virtual ~SomeClass() {}
  
          void doSomething() {;}
  };
  </code></pre>
  
  <p>Obviously this is a simple example, not an actual class.  Anyways, I am finding that using code such as:</p>
  
  <pre><code>SomeClass::getInstance().doSomething();
  </code></pre>
  
  <p>Will create more than one instance of SomeClass.  I am thinking this may be due to the fact that it is being used outside my library (.a) file as well as internally.  For example, I am using a UI library not written by myself which is separately compiled and to which I am making additions.  Some of these additions utilize singletons which are also being used in my .a library.</p>
  
  <p>Is the separate compilation causing this?  Something else?</p>
  
  <p>The only way I have managed to get around the issue is to create a global object in my main.cpp file which I initialize with any singletons I will need.  Then all code accesses this common global object with calls such as:</p>
  
  <pre><code>GlobalObject::getSomeClass().doSomething()
  </code></pre>
  
  <p>I hate having to add an additional method to this object every time I create another singleton.  Plus the syntax seems clearer and more familiar using the first access method:</p>
  
  <pre><code>SomeClass::getInstance().doSomething();
  </code></pre>
  
  <p>Please let me know if you have any thoughts, opinions, etc.</p>
  
  <p>Thanks.</p>