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

C++单体模式的几个总结

2009-05-27 14:43 351 查看
1 通用简单单体
#include
using namespace std;
class Singleton
{
private:
static Singleton s;
int i;
Singleton(int x) : i(x) {}
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()
{
Singleton& s = Singleton::instance();
cout <<s.getValue() << endl;
Singleton& s2 = Singleton:: instance();
s2.setValue(9);
cout << s.getValue() << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: