pneave

C++11 Singleton

Jan 15th, 2018 (edited)
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. class Singleton {
  2.  public:
  3.   static Singleton& Instance() {
  4.     // Since it's a static variable, if the class has already been created,
  5.     // it won't be created again.
  6.     // And it **is** thread-safe in C++11.
  7.     static Singleton myInstance;
  8.  
  9.     // Return a reference to our instance.
  10.     return myInstance;
  11.   }
  12.  
  13.   // delete copy and move constructors and assign operators
  14.   Singleton(Singleton const&) = delete;             // Copy construct
  15.   Singleton(Singleton&&) = delete;                  // Move construct
  16.   Singleton& operator=(Singleton const&) = delete;  // Copy assign
  17.   Singleton& operator=(Singleton &&) = delete;      // Move assign
  18.  
  19.   // Any other public methods.
  20.  
  21.  protected:
  22.   Singleton() {
  23.     // Constructor code goes here.
  24.   }
  25.  
  26.   ~Singleton() {
  27.     // Destructor code goes here.
  28.   }
  29.  
  30.  // And any other protected methods.
  31. }
Add Comment
Please, Sign In to add comment