5,530
社区成员
发帖
与我相关
我的任务
分享
template <typename T>
class Singleton
{
class Garbo
{
public:
~Garbo()
{
if(Singleton<T>::m_){
delete Singleton<T>::m_;
Singleton<T>::m_ = NULL;
}
}
};
public:
static T* GetInstance()
{
garbo; // 无用代码,但会让编译器帮你生成garbo
if(!m_)
{
cs.Lock();
if(!m_)
m_ = new T();
cs.UnLock();
}
return m_;
}
~Singleton(){}
public:
Singleton(){}
static T* m_;
static CriticalSection cs;
static Garbo garbo;
};