#include <stdio.h>
#include <stdlib.h>
class CSingleton
{
public:
static CSingleton* GetInstance()
{
if ( m_pInstance == NULL ) { //判断是否第一次调用
m_pInstance = new CSingleton();
atexit(CSingleton::Release_CSingleton);
}
return m_pInstance;
}
static void Release_CSingleton()
{
if (m_pInstance != NULL) {
delete m_pInstance;
m_pInstance = NULL;
}
}
private:
CSingleton(){};
static CSingleton *m_pInstance;
};
CSingleton* CSingleton::m_pInstance = NULL;
int main()
{
CSingleton* p = CSingleton::GetInstance();
return 0;
}
利用atexit实现单例模式自动析构
最新推荐文章于 2022-10-21 20:11:51 发布