singleton.cc

来自「day77_1187669327.zip」· CC 代码 · 共 36 行

CC
36
字号
#include <iostream>using namespace std;class Single{	int data;	Single(){}	Single(const Single& o){}	//static int s;	static Single obj;public:	static Single& getInstance(){		return obj;	}	void set(int d){		data = d;	}	void show(){		cout << "this=" << this << ", ";		cout << "data=" << data << endl;	}};//int Single::s;Single Single::obj;int main(){	Single& s1=Single::getInstance();	Single& s2=Single::getInstance();	s1.set(100);	s1.show();	s2.show();	//Single s3;//ERROR!!	//Single s4(s1);//ERROR!!	//Single s5=s1;//ERROR!!}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?