Introduction to OOP | Chapter 24: Design Patterns : | next | previous | audio | real | text |
Solution: Make the constructor private, have a method that returns just one instance, which is held inside the class itself.
class SingletonClass { public: static SingletonClass * oneAndOnly () { return theOne; } private: static SingletonClass * theOne; SingletonClass () { ... } }; // static initialization SingletonClass * SingletonClass::theOne = new SingletonClass();