思路:构造函数私有,对外仅提供一个公开的函数来获取该实例。
同时考虑到析构的时候。
class CSingleton
{
public:
static CSingleton getInstance()
{
if(m_pInstance == null)
{
m_pInstance = new CSingleton();
}
return m_pInstance;
}
private:
CSingleton();
static CSingleton * m_pInstance;
class CGarbo //when CGarbo destructed, it will delte CSingleton::m_pInstance
{
public:
~CGarbo()
{
if(CSingleton::m_pInstance != NULL)
{
delete CSingleton::m_pInstance;
}
}
}
static CGarbo m_garbo;
}
博主Github:https://github.com/linmq 欢迎访问。
参考资料:http://blog.csdn.net/jackystudio/article/details/11764493
http://blog.csdn.net/chenyufeng1991/article/details/47925457
http://blog.csdn.net/crayondeng/article/details/24853471