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

C++单例模型的例子

2014年01月02日 ⁄ 综合 ⁄ 共 535字 ⁄ 字号 评论关闭
#include <iostream>
#include <new>
using namespace std;

class CGlobalInstance
{
  private:
       CGlobalInstance();
       virtual ~CGlobalInstance();
       static CGlobalInstance* m_this;
  public:  
   static CGlobalInstance* get_instance();
   void out();
};


CGlobalInstance::CGlobalInstance()
{

}


CGlobalInstance::~CGlobalInstance()
{

}

void CGlobalInstance::out()
{
    cout<<"hi man"<<endl;
}

CGlobalInstance* CGlobalInstance::get_instance()
{
   if (NULL == m_this)
   {
     m_this = new CGlobalInstance();
   }
   return m_this;
}

CGlobalInstance* CGlobalInstance::m_this = NULL;

int main(void)
{
  CGlobalInstance::get_instance()->out();
}

抱歉!评论已关闭.