/ Published in: C++
Note: This doesn't work in multi-threaded environments, check http://en.wikipedia.org/wiki/Singleton_pattern#C.2B.2B
Singleton class model
then:
SingletonClass *pSC = SingletonClass::get_singleton_instance();
pSC->any_public_function();
or: SingletonClass::get_singleton_instance()->any_public_function();
at the end of the program don't forget: SingletonClass::destroy();
Singleton class model
then:
SingletonClass *pSC = SingletonClass::get_singleton_instance();
pSC->any_public_function();
or: SingletonClass::get_singleton_instance()->any_public_function();
at the end of the program don't forget: SingletonClass::destroy();
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//.h class SingletonClass { static SingletonClass *singleton; SingletonClass(); public: static SingletonClass * get_singleton_instance(); static void destroy(); ~SingletonClass(); }; //.cpp SingletonClass *SingletonClass::singleton = NULL; SingletonClass * SingletonClass::get_singleton() { if( singleton == NULL ) singleton = new SingletonClass(); return singleton; } void SingletonClass::destroy() { if( singleton != NULL ) delete singleton; singleton = NULL; } SingletonClass::SingletonClass() { singleton = this; } SingletonClass::~SingletonClass() { }