现在的位置: 首页 > 综合 > 正文

Singleton (C++实现)

2012年10月04日 ⁄ 综合 ⁄ 共 606字 ⁄ 字号 评论关闭

// Singleton.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

using namespace std;

class Singleton
{
public:
 Singleton(){}
 virtual ~Singleton(){}

 

 static Singleton* GetInstancePtr()
 {
  if(NULL==m_pStatic)
  {
   m_pStatic=new Singleton();
  }
  return m_pStatic;
 }

 

 

 static  Singleton GetInstance()
 {
  return *GetInstancePtr();
 }

 

 

 void test()
 {
  cout<<"Singleton patton test!"<<endl;
 
 }

 

private:
 static Singleton * m_pStatic;

};

 

Singleton * Singleton::m_pStatic=NULL;

/////////////////////////////////////////////

int _tmain(int argc, _TCHAR* argv[])
{
 Singleton::GetInstancePtr()->test();
 Singleton::GetInstance().test();
 return 0;
}

 

抱歉!评论已关闭.