-
Notifications
You must be signed in to change notification settings - Fork 0
/
singleton.cc
50 lines (39 loc) · 1.17 KB
/
singleton.cc
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
49
50
#include <iostream>
// class Singleton {
// public:
// Singleton(const Singleton& other) = delete;
// Singleton& operator=(const Singleton& other) = delete;
// static Singleton* GetInstance(const std::string& str) {
// if (single_ == nullptr) {
// single_ = new Singleton(str);
// }
// return single_;
// }
// std::string GetValue() { return value_; }
// private:
// Singleton(const std::string& str) : value_(str) {}
// ~Singleton() = default;
// static Singleton* single_;
// std::string value_;
// };
// Singleton* Singleton::single_ = nullptr;
class Singleton {
public:
~Singleton() = default;
Singleton(const Singleton& s) = delete;
Singleton& operator=(const Singleton& s) = delete;
static Singleton& GetInstance() {
static Singleton singleton;
return singleton;
}
private:
Singleton() = default;
};
int main() {
// Singleton* s1 = Singleton::GetInstance("foo");
// std::cout << s1->GetValue() << std::endl;
// Singleton* s2 = Singleton::GetInstance("haha");
// std::cout << s2->GetValue() << std::endl;
Singleton& s = Singleton::GetInstance();
return 0;
}