/* * singleton.cpp * * Singleton example. Ensure a class has only one instance * and provide a global point of access. */ #include class Singleton { public: static Singleton& instance() { static Singleton s; return s; } void operation() { std::cout << "Singleton " << this << "\n"; } ~Singleton() {} protected: Singleton() {}; }; int main() { Singleton& s1 = Singleton::instance(); s1.operation(); Singleton& s2 = Singleton::instance(); s2.operation(); }