pneave icon

C++11 Singleton

pneave | PRO | 05/01/21 04:22:39 PM UTC (Edited) | 0 ⭐ | 319 👁️ | Never ⏰ | []
C++ |

860 B

|

None

|

0 👍

/

0 👎

class Singleton {
 public:
  static Singleton& Instance() {
    // Since it's a static variable, if the class has already been created,
    // it won't be created again.
    // And it **is** thread-safe in C++11.
    static Singleton myInstance;
 
    // Return a reference to our instance.
    return myInstance;
  }
 
  // delete copy and move constructors and assign operators
  Singleton(Singleton const&) = delete;             // Copy construct
  Singleton(Singleton&&) = delete;                  // Move construct
  Singleton& operator=(Singleton const&) = delete;  // Copy assign
  Singleton& operator=(Singleton &&) = delete;      // Move assign
 
  // Any other public methods.
 
 protected:
  Singleton() {
    // Constructor code goes here.
  }
 
  ~Singleton() {
    // Destructor code goes here.
  }
 
 // And any other protected methods.
}

Comments