天天看点

设计模式:单例模式(懒汉式)单例模式(Singleton)(懒汉式)

单例模式(Singleton)(懒汉式)

#include "iostream"
using namespace std;

class CSingleton
{
public:
	CSingleton(){};
	~CSingleton(){};
	static CSingleton *GetInstance()
	{
		static CSingleton m_Instance;
		return &m_Instance;
	}
	void show()
	{
		cout << "我是单例模式" << endl;
	}
};

int main()
{
	CSingleton *s1 = CSingleton::GetInstance();
	s1->show();
	CSingleton *s2 = CSingleton::GetInstance();
	s2->show();
	cout << s1 << endl;
	cout << s2 << endl;
	CSingleton::GetInstance()->show();
	cout << CSingleton::GetInstance() << endl;
	system("pause");
	return 1;
}
/*输出结果*/
我是单例模式
我是单例模式
011C0330
011C0330
我是单例模式
011C0330
请按任意键继续. . .
           

设计模式

继续阅读