懒汉
template<typename T>
class Singleton
{
public:
T* GetSingletonPtr()
{
if(_instance)return _instance;
else return _instance = new T;
}
~Singleton()
{
if(_instance)
{
delete _instance;
_instance = NULL;
}
}
private:
static T* _instance;
}
template<typename T>
T* Singleton<T>::_instance = NULL;
饿汉
template<typename T>
class Singleton
{
public:
T* GetSingletonPtr(){if(_instance)return _instance;}
~Singleton()
{
if(_instance)
{
delete _instance;
_instance = NULL;
}
}
}
template<typename T>
T* Singleton<T>::_instance = new T;
备注
总觉得哪有问题,后面再确定一下
附一个其他的单例博客(带线程)
带线程安全的单例模式