title: Cannot access private member in singleton class destructor
body: <p>I'm trying to implement this singleton class. But I encountered this error:</p>
  
  <p>'Singleton::~Singleton': cannot access private member declared in class 'Singleton'
  This is flagged in the header file, the last line which contains the closing brace.</p>
  
  <p>Can somebody help me explain what is causing this problem?
  Below is my source code.</p>
  
  <p>Singleton.h:</p>
  
  <pre><code>
  class Singleton
  {
  public:
      static Singleton* Instance()
      {
          if( !pInstance )
          {
              if( destroyed )
              {
                  // throw exception
              }
              else
              {
                  Create();
              }
  
          }
          return pInstance;
      }
  private:
      static void Create()
      {
          static Singleton myInstance;
          pInstance = &myInstance;
      }
      Singleton() {}
      Singleton( const Singleton& );
      Singleton& operator=( const Singleton& );
      ~Singleton() 
      {
          pInstance = 0;
          detroyed = false;
      }
  
      static Singleton* pInstance;
      static bool destroyed;
  };
  </code></pre>
  
  <p>Singleton.cpp:</p>
  
  <pre><code>
  Singleton* Singleton::pInstance = 0;
  bool Singleton::destroyed = false;
  </code></pre>
  
  <p>Inside my main function:</p>
  
  <pre><code>
  Singleton* s = Singleton::Instance();
  </code></pre>
  
  <p>If I make the destructor as public, then the problem disappears. But a book (Modern C++ Design) says it should be private to prevent users from deleting the instance. I actually need to put some code for cleanup for pInstance and destroyed inside the destructor.</p>
  
  <p>By the way, I'm using Visual C++ 6.0 to compile.</p>