您的位置:首页 > 其它

[转]【创建型】 之 单件模式

2010-12-30 21:44 246 查看
/***********************************************
* 【创建型】 之 单件模式
*
*
*摘自c++ 编程思想 第2卷 384页  2010-12-30
************************************************/
#include <iostream>
using namespace std;
class Singleton
{
private:
static Singleton s;

int i;
Singleton (int x):i(x) { };

Singleton& operator=(Singleton&); //disabled
Singleton (const Singleton&); 	   //disabled

public:
static Singleton  &instance(){
return s;
}
int getValue(){
return i;
}
void setValue(int x){
i =x;
}
}

Singleton::s(47);

int main(){

Singleton& s = Singleton::instance();
cout <<s.getValue()<<endl;

Singleton& s2 = Singleton::instance();
s2.setValue(9);
cout << s.getValue()<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: