Welcome to tiger's blog!

What lead to success, what we are seeking...
posts - 47, comments - 23, trackbacks - 0, articles - 8
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

设计模式(Singleton)

Posted on 2007-04-16 10:57 tiger 阅读(337) 评论(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;
 }
}


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理