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

c++单例模式

2018年02月07日 ⁄ 综合 ⁄ 共 541字 ⁄ 字号 评论关闭
#include <iostream>
#include <stdlib.h>

class Singleton 
{
private:
	Singleton();	
public:
	virtual ~Singleton();
	static Singleton *GetInstance();

private:
	static Singleton *m_pInstance;
};

Singleton *Singleton::m_pInstance = NULL;

Singleton::Singleton()
{
	std::cout << "class Singleton - Constructor" << std::endl;
}

Singleton::~Singleton()
{
	delete m_pInstance;
}

Singleton *Singleton::GetInstance()
{
	if (m_pInstance == NULL)
	{
		m_pInstance = new Singleton();
	}

	return m_pInstance;
}


int main()
{
	Singleton *Obj1 = Singleton::GetInstance();
	Singleton *Obj2 = Singleton::GetInstance();

	system("pause");
	return 0;
}



【上篇】
【下篇】

抱歉!评论已关闭.