template <typename T> class CSingleton { static T* ms_Singleton; public: CSingleton(void) { assert(!ms_Singleton); int offset = (int)(T*)1 - (int)(CSingleton <T>*)(T*)1; ms_Singleton = (T*)((int)this + offset); } ~CSingleton(void) { assert(ms_Singleton); ms_Singleton = 0; } static T* GetSingleton(void) { assert(ms_Singleton); return ms_Singleton; } static T* GetSingletonPtr(void) { if(ms_Singleton == NULL) { return ms_Singleton; } assert(ms_Singleton); return ms_Singleton; } static T& GetSingletonInstance(void) { assert(ms_Singleton); return(*ms_Singleton); } }; template <typename T> T* CSingleton <T>::ms_Singleton = 0;