Skip to content

Commit

Permalink
修复设计模式-单例的一些错误
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Super committed Jan 13, 2022
1 parent 388564c commit b3dd817
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
3 changes: 2 additions & 1 deletion design_pattern/singleton/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private:
static singleton *p;
static mutex lock_;
public:
singleton *instance();
static singleton *instance();

// 实现一个内嵌垃圾回收类
class CGarbo
Expand All @@ -131,6 +131,7 @@ public:

singleton *singleton::p = nullptr;
singleton::CGarbo Garbo;
std::mutex singleton::lock_;

singleton* singleton::instance() {
if (p == nullptr) {
Expand Down
3 changes: 2 additions & 1 deletion design_pattern/singleton/dcl_singleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class singleton {
static singleton *p;
static mutex lock_;
public:
singleton *instance();
static singleton *instance();

// 实现一个内嵌垃圾回收类
class CGarbo
Expand All @@ -31,6 +31,7 @@ class singleton {

singleton *singleton::p = nullptr;
singleton::CGarbo Garbo;
std::mutex singleton::lock_;

singleton* singleton::instance() {
if (p == nullptr) {
Expand Down
6 changes: 3 additions & 3 deletions design_pattern/singleton/static_local_singleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class singleton {
static singleton *p;
singleton() {}
public:
singleton *instance();
static singleton &instance();
};

singleton *singleton::instance() {
singleton &singleton::instance() {
static singleton p;
return &p;
return p;
}


0 comments on commit b3dd817

Please sign in to comment.