[求助]Singleton的问题
问题如下:
template<typename T>
class Singleton
{
static T* ms_Singleton;
public:
Singleton()
{
assert(!ms_Singleton);
int offset=(int)(T*)1-(int)(Singleton<T>*)(T*)1; //这里的作用是什么???
ms_Singleton=(T*)((int)this+offset);
}
~Singleton()
{
assert(ms_Singleton);
ms_Singleton=0;
}
static T GetSingleton()
{
assert(ms_Singleton);
return (*ms_Singleton);
}
static T* GetSingletonPtr()
{
assert(ms_Singleton);
return ms_Singleton;
}
};
template <typename T> T* Singleton<T>::ms_Singleton=0;
class MyClass:public Singleton<MyClass>
{
};