您的位置:首页 > 编程语言 > C语言/C++

《C++编程思想》读书笔记一 设计模式(待续。。。)

2013-03-05 09:45 218 查看

单件

#include <iostream>
using namespace std;

class Singleton
{
static Singleton s;
static int b;
int i;
Singleton(int x): i(x){};
Singleton& operator=(Singleton&);
Singleton(const Singleton&);

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

Singleton Singleton::s(47);

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