Skip to content

Commit

Permalink
Merge pull request Light-City#196 from L-Super/master
Browse files Browse the repository at this point in the history
修复设计模式-单例的一些错误
  • Loading branch information
Light-City committed Apr 10, 2022
2 parents 5d23c9d + 646f9e1 commit 88083a0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 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
9 changes: 5 additions & 4 deletions design_pattern/singleton/static_local_singleton.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//
// Created by light on 20-2-7.
// 在C++11标准下,《Effective C++》提出了一种更优雅的单例模式实现,使用函数内的 local static 对象。
// 这种方法也被称为Meyers' Singleton。
//

#include <iostream>
Expand All @@ -8,15 +10,14 @@ using namespace std;

class singleton {
private:
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 88083a0

Please sign in to comment.