首页 > 学院 > 开发设计 > 正文

c++单例模式实现源码

2019-11-14 09:53:56
字体:
来源:转载
供稿:网友
#define _CRT_SECURE_NO_WARNINGS#include <iostream>using namespace std;class Instance{PRivate:	Instance(){}	/*私有化复制构造函数*/	Instance(const Instance&){}	/*私有化=操作符*/	Instance& Operator=(const Instance&){}public:	static Instance *getInstance()	{		return Singleton;	}public:	static Instance *Singleton;};/*初始化*/Instance *Instance::Singleton = new Instance;/*测试*/void test(){	Instance *p1 = Instance::getInstance();	Instance *p2 = Instance::getInstance();	if (p1 == p2)	{		cout << "p1 = p2" << endl;	}	else	{		cout << "p1 != p2" << endl;	}}int main() {	test();	system("pause");	return EXIT_SUCCESS;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表