Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>See this article for a simple design for a lazy evaluated with guaranteed destruction singleton:<br> <a href="https://stackoverflow.com/questions/270947/can-any-one-provide-me-a-sample-of-singleton-in-c/271104#271104">Can any one provide me a sample of Singleton in c++?</a> </p> <p>The classic lazy evaluated and correctly destroyed singleton.</p> <pre><code>class S { public: static S&amp; getInstance() { static S instance; // Guaranteed to be destroyed. // Instantiated on first use. return instance; } private: S() {} // Constructor? (the {} brackets) are needed here. // C++ 03 // ======== // Don't forget to declare these two. You want to make sure they // are unacceptable otherwise you may accidentally get copies of // your singleton appearing. S(S const&amp;); // Don't Implement void operator=(S const&amp;); // Don't implement // C++ 11 // ======= // We can use the better technique of deleting the methods // we don't want. public: S(S const&amp;) = delete; void operator=(S const&amp;) = delete; // Note: Scott Meyers mentions in his Effective Modern // C++ book, that deleted functions should generally // be public as it results in better error messages // due to the compilers behavior to check accessibility // before deleted status }; </code></pre> <p>See this article about when to use a singleton: (not often)<br> <a href="https://stackoverflow.com/questions/86582/singleton-how-should-it-be-used">Singleton: How should it be used</a></p> <p>See this two article about initialization order and how to cope:<br> <a href="https://stackoverflow.com/questions/211237/c-static-variables-initialisation-order/211307#211307">Static variables initialisation order</a><br> <a href="https://stackoverflow.com/questions/335369/finding-c-static-initialization-order-problems/335746#335746">Finding C++ static initialization order problems</a> </p> <p>See this article describing lifetimes:<br> <a href="https://stackoverflow.com/questions/246564/what-is-the-lifetime-of-a-static-variable-in-a-c-function">What is the lifetime of a static variable in a C++ function?</a> </p> <p>See this article that discusses some threading implications to singletons:<br> <a href="https://stackoverflow.com/questions/449436/singleton-instance-declared-as-static-variable-of-getinstance-method/449823#449823">Singleton instance declared as static variable of GetInstance method</a></p> <p>See this article that explains why double checked locking will not work on C++:<br> <a href="https://stackoverflow.com/questions/367633/what-are-all-the-common-undefined-behaviour-that-c-programmer-should-know-about/367690#367690">What are all the common undefined behaviours that a C++ programmer should know about?</a><br> <a href="http://www.drdobbs.com/cpp/c-and-the-perils-of-double-checked-locki/184405726" rel="noreferrer">Dr Dobbs: C++ and The Perils of Double-Checked Locking: Part I</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload