修复设计模式-单例的一些错误
This commit is contained in:
parent
388564c9d9
commit
b3dd817d4f
@ -114,7 +114,7 @@ private:
|
|||||||
static singleton *p;
|
static singleton *p;
|
||||||
static mutex lock_;
|
static mutex lock_;
|
||||||
public:
|
public:
|
||||||
singleton *instance();
|
static singleton *instance();
|
||||||
|
|
||||||
// 实现一个内嵌垃圾回收类
|
// 实现一个内嵌垃圾回收类
|
||||||
class CGarbo
|
class CGarbo
|
||||||
@ -131,6 +131,7 @@ public:
|
|||||||
|
|
||||||
singleton *singleton::p = nullptr;
|
singleton *singleton::p = nullptr;
|
||||||
singleton::CGarbo Garbo;
|
singleton::CGarbo Garbo;
|
||||||
|
std::mutex singleton::lock_;
|
||||||
|
|
||||||
singleton* singleton::instance() {
|
singleton* singleton::instance() {
|
||||||
if (p == nullptr) {
|
if (p == nullptr) {
|
||||||
|
@ -14,7 +14,7 @@ private:
|
|||||||
static singleton *p;
|
static singleton *p;
|
||||||
static mutex lock_;
|
static mutex lock_;
|
||||||
public:
|
public:
|
||||||
singleton *instance();
|
static singleton *instance();
|
||||||
|
|
||||||
// 实现一个内嵌垃圾回收类
|
// 实现一个内嵌垃圾回收类
|
||||||
class CGarbo
|
class CGarbo
|
||||||
@ -31,6 +31,7 @@ public:
|
|||||||
|
|
||||||
singleton *singleton::p = nullptr;
|
singleton *singleton::p = nullptr;
|
||||||
singleton::CGarbo Garbo;
|
singleton::CGarbo Garbo;
|
||||||
|
std::mutex singleton::lock_;
|
||||||
|
|
||||||
singleton* singleton::instance() {
|
singleton* singleton::instance() {
|
||||||
if (p == nullptr) {
|
if (p == nullptr) {
|
||||||
|
@ -11,12 +11,12 @@ private:
|
|||||||
static singleton *p;
|
static singleton *p;
|
||||||
singleton() {}
|
singleton() {}
|
||||||
public:
|
public:
|
||||||
singleton *instance();
|
static singleton &instance();
|
||||||
};
|
};
|
||||||
|
|
||||||
singleton *singleton::instance() {
|
singleton &singleton::instance() {
|
||||||
static singleton p;
|
static singleton p;
|
||||||
return &p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user