forked from mrchuanxu/RegularNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton.cpp
More file actions
37 lines (34 loc) · 756 Bytes
/
singleton.cpp
File metadata and controls
37 lines (34 loc) · 756 Bytes
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
#include <iostream>
using namespace std;
/***单例模式
* 自己的事情自己做!
* 保证线程安全
* ***/
class Father{
public:
static Father* getFather();
static void say(){
cout << "i am father" << endl;
}
private:
Father(){};
Father& operator=(const Father&);
Father(const Father&);
static Father* father;
virtual ~Father(){
delete father;
father = nullptr;
}
};
Father* Father::father = nullptr;
Father* Father::getFather(){
if(nullptr == father){
father = new Father();
}
return father;
}
int main(){
Father *iamfather = Father::getFather();
iamfather->say();
return 0;
}