class singleton
{
private:
singleton(){ na = 1; nb = 2; };
static singleton* pInstance;
public:
static singleton* GetInstance()
{
if (pInstance == NULL) pInstance = new singleton();
return pInstance;
}
static void Delete()
{
if (pInstance != NULL) { delete pInstance; pInstance = NULL; }
}
public:
int na;
int nb;
};
#define gpServer singleton::GetInstance()
singleton* singleton::pInstance = NULL;
gpServer->na = 4;
gpServer->nb = 6;
int nNum = gpServer->na; //4
nNum = gpServer->nb; //6
singleton::Delete(); //用完记得删除
此单例模式示例可用于模拟全局变量!