1 #include <iostream> // 标准的输入输出
2 using namespace std;
3
4 // 单例模式案例
5 class Chairman {
6 private:
7 Chairman()
8 {
9 ¦ cout << "创建国家主席" << endl;
10 }
11
12 Chairman(const Chairman& a)
13 {
14 }
15
16 static Chairman* singman;
17
18 public:
19 // 提供get方法用于外部访问
20 static Chairman* getInstance()
21 {
22 ¦ return singman;
23 }
24 };
25 //初始化singman
26 Chairman* Chairman::singman = new Chairman;
27
28 void test01()
29 {
30 // 取对象
31 Chairman* cm = Chairman::getInstance();
32 }
33 int main()
34 {
35 cout << "main函数调用" << endl;
36 test01();
37 return 0;
38 }
c++ 单例模型案例
最新推荐文章于 2024-10-10 22:04:37 发布