The following example demonstrates how to have only one instance of a class at a time.
class MySingleTon{public: static MySingleTon * GetObject() { static MySingleTon obj; return &obj; } ... //other interface functions here ...private: MySingleTon();};
First, declare the constructor as private, so no one can create an instance. Next, declare a static method, like GetObject in the code above. Any client can use the only instance of the class, as shown below:
#include