title: C++ singleton with private constructor
body: <p>I need singleton with a application lifetime, guaranteed creation/destruction and static access to it.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  
  #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
      TypeName(const TypeName&amp;);             \
      void operator=(const TypeName&amp;)
  
  #define M() C::sM()
  #define M2() C::sM2()
  
  using namespace std;
  
  class C {
    private:
      static C* s;
  
      ~C() { cout &lt;&lt; "~C()" &lt;&lt; endl; }
      static C* instance() { 
          if (s==NULL) { s=new C(); }
          cout &lt;&lt; "instance()=" &lt;&lt; s &lt;&lt; endl; return s; 
      }
      static void cleanUp() { delete s; }
      void m() { cout &lt;&lt; "m()" &lt;&lt; endl; }
      void m2() { cout &lt;&lt; "m2()" &lt;&lt; endl; }
      DISALLOW_COPY_AND_ASSIGN(C);
    public:
      C() { 
          cout &lt;&lt; "C()" &lt;&lt; endl; if (s==NULL) { 
              s=this; atexit(&amp;cleanUp); 
              cout &lt;&lt; "cleanUp is installed" &lt;&lt; endl; 
          } else { 
              cout &lt;&lt; "cleanUp is not installed" &lt;&lt; endl; 
          } 
      }
      void* operator new(size_t sz) { 
          void* p=NULL; if (s==NULL) { p=new char[sz]; } else { p=s; }
          cout &lt;&lt; "new(" &lt;&lt; sz &lt;&lt; ")=" &lt;&lt; p &lt;&lt; endl;
          return p;
      }
      void operator delete(void* p, size_t sz) { 
          cout &lt;&lt; "delete(" &lt;&lt; sz &lt;&lt; "," &lt;&lt; p &lt;&lt; ")" &lt;&lt; endl;
          if (p) delete[] static_cast&lt;char*&gt;(p); s=NULL; 
      }
      void static sM() { cout &lt;&lt; "sM()" &lt;&lt; endl; instance()-&gt;m(); }
      void static sM2() { cout &lt;&lt; "sM2()" &lt;&lt; endl; instance()-&gt;m2(); }
  };
  
  C* C::s = NULL;
  
  int main() {
      M();
      M2();
      C* p1 = new C();
      C* p2 = new C();
  }  
  </code></pre>
  
  <p>But I don't know how to get rid of g++ warning:</p>
  
  <pre><code>test.cpp: In static member function 'static void C::operator delete(void*, size_t)':
  test.cpp:22: warning: deleting 'void*' is undefined
  </code></pre>
  
  <p>If I write C* instead of void*, destructor start calling itself in infinite loop. Can anybody help me to get clean code without warnings? C++98 of course.</p>