Published in: C++
Singleton class model then: SingletonClass *pSC = SingletonClass::getsingletoninstance(); pSC->anypublicfunction(); or: SingletonClass::getsingletoninstance()->anypublicfunction();
//.h class SingletonClass { static SingletonClass *singleton; SingletonClass(); public: static SingletonClass * get_singleton_instance(); ~SingletonClass(); }; //.cpp SingletonClass *SingletonClass::singleton = NULL; SingletonClass * SingletonClass::get_singleton() { if( singleton == NULL ) singleton = new SingletonClass(); return singleton; } SingletonClass::SingletonClass() { singleton = this; } SingletonClass::~SingletonClass() { }
Comments
Subscribe to comments
You need to login to post a comment.

Seeing how this is a "Popular" code snippet, I wanted to warn everybody, that it is not good for multithreaded environment. For more details: http://www.aristeia.com/Papers/DDJJulAug2004revised.pdf
Yo may find usefull information about Singleton here: http://sourcemaking.com/design_patterns/singleton