title: Singleton pattern in C++
body: <p>I have a question about the singleton pattern.</p>
  
  <p>I saw two cases concerning the static member in the singleton class.</p>
  
  <p>First it is an object, like this</p>
  
  <pre><code>class CMySingleton
  {
  public:
    static CMySingleton&amp; Instance()
    {
      static CMySingleton singleton;
      return singleton;
    }
  
  // Other non-static member functions
  private:
    CMySingleton() {}                                  // Private constructor
    ~CMySingleton() {}
    CMySingleton(const CMySingleton&amp;);                 // Prevent copy-construction
    CMySingleton&amp; operator=(const CMySingleton&amp;);      // Prevent assignment
  };
  </code></pre>
  
  <p>One is an pointer, like this</p>
  
  <pre><code>class GlobalClass
  {
      int m_value;
      static GlobalClass *s_instance;
      GlobalClass(int v = 0)
      {
          m_value = v;
      }
    public:
      int get_value()
      {
          return m_value;
      }
      void set_value(int v)
      {
          m_value = v;
      }
      static GlobalClass *instance()
      {
          if (!s_instance)
            s_instance = new GlobalClass;
          return s_instance;
      }
  };
  </code></pre>
  
  <p>What's the difference between the two cases? Which one is correct?</p>