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++单体模式的几个总结
最新推荐文章于 2022-03-13 13:34:35 发布