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;
}
C++单体模式的几个总结
最新推荐文章于 2024-11-22 15:44:44 发布