变成思想第二卷的一段单态例子 有点不明白
代码如下 不明白的地方在代码中添加注释了
// 2013030901.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class Singleton
{
private:
static Singleton s;
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); // 这是什么意思?初始化一个Singleton吗 s不是私有的吗?Singleton Singleton是什么意思?
int main()
{
Singleton& s = Singleton::instance(); // 为什么用Singleton& s "&"是什么意思?
cout<<s.getValue()<<endl;
Singleton& s2 = Singleton::instance();
s2.setValue(1);
cout<<s.getValue()<<endl;
return 0;
}