C++单例模型的例子

2014-11-24 12:24:44 · 作者: · 浏览: 3
#include
#include
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"< }

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();
}

摘自 工作记录--创造或收集原创