class Singleton{
public:
~Singleton();
static Singleton* getInstance();
static void destroy();
private:
static Singleton* pInstance;
Singleton();
};
Singleton* Singleton::getInstance()
{
if(pInstance == 0)
{
pInstance = new Singleton();
}
return pInstance;
}
以上这种写法的单一模式,在程序退出的时候不会自动析构,必须手动delete 或者调用destory()函数。
class Singleton{
public:
~Singleton();
static Singleton* getInstance();
private:
Singleton();
};
Singleton* Singleton::getInstance()
{
static Singleton pInstance;
}
return &pInstance;
}