Posted on 2007-04-16 10:57
tiger 阅读(347)
评论(0) 编辑 收藏 引用
#include <iostream>
using namespace std;
class CSingleton
{
private:
CSingleton();
public:
virtual ~CSingleton();
public:
static CSingleton *GetInstance();
private:
static CSingleton *s_pSingleton;
};
CSingleton *CSingleton::s_pSingleton = NULL;
CSingleton::CSingleton()
{
}
CSingleton::~CSingleton()
{
if(CSingleton::s_pSingleton != NULL)
{
delete CSingleton::s_pSingleton;
CSingleton::s_pSingleton = NULL;
}
}
CSingleton *CSingleton::GetInstance()
{
if(CSingleton::s_pSingleton == NULL)
{
CSingleton::s_pSingleton = new CSingleton();
}
return CSingleton::s_pSingleton;
}
void main()
{
for(int i = 0; i < 10; i++)
{
CSingleton *pSingleton = CSingleton::GetInstance();
cout<<i<<"\t"<<pSingleton<<endl;
}
}