-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPresident.cpp
48 lines (39 loc) · 1.36 KB
/
President.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
using namespace std;
class President
{
private:
President() {};
President(const President&);
const President& operator=(const President&);
string Name;
public:
static President& GetInstance()
{
static President OnlyInstance;
return OnlyInstance;
}
string GetName()
{
return Name;
}
void SetName(string inputName)
{
Name = inputName;
}
};
int main()
{
President& OnlyInstance = President::GetInstance();
OnlyInstance.SetName("aiter liu");
//President SecondInstance; //默认的constructor ,不允许
//President* ThirdInstance = new President(); //默认的constructor ,不允许.这个和上面的区别是,在堆上创建对象
//President FourInstance = OnlyInstance;//不能使用copy constructor,private
//OnlyInstance = President::GetInstance(); 这个将这个引用重新复制operator=,这里也不能访问,private
//President SameInstance = President::GetInstance(); 不加& ,这个就不是引用,就会调用constructor,但是这里是private的
President& SameInstance = President::GetInstance(); //& 只是引用
SameInstance.SetName("leya liu");
cout << President::GetInstance().GetName() << endl;
return 0;
}