classS { public: // Guaranteed to be destroyed. // Instantiated on first use. static S &getInstance(){ static S instance; return instance; }
private: S() {}
// C++ 03 // ======== // Don't forget to declare these two. You want to make sure they // are inaccessible(especially from outside), otherwise, you may accidentally get copies of // your singleton appearing. S(S const &); // Don't Implement voidoperator=(S const &); // Don't implement
// C++ 11 // ======= // We can use the better technique of deleting the methods // we don't want. public: S(S const &) = delete; voidoperator=(S const &) = 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 };