commit
88083a0b69
@ -114,7 +114,7 @@ private:
|
||||
static singleton *p;
|
||||
static mutex lock_;
|
||||
public:
|
||||
singleton *instance();
|
||||
static singleton *instance();
|
||||
|
||||
// 实现一个内嵌垃圾回收类
|
||||
class CGarbo
|
||||
@ -131,6 +131,7 @@ public:
|
||||
|
||||
singleton *singleton::p = nullptr;
|
||||
singleton::CGarbo Garbo;
|
||||
std::mutex singleton::lock_;
|
||||
|
||||
singleton* singleton::instance() {
|
||||
if (p == nullptr) {
|
||||
|
@ -14,7 +14,7 @@ private:
|
||||
static singleton *p;
|
||||
static mutex lock_;
|
||||
public:
|
||||
singleton *instance();
|
||||
static singleton *instance();
|
||||
|
||||
// 实现一个内嵌垃圾回收类
|
||||
class CGarbo
|
||||
@ -31,6 +31,7 @@ public:
|
||||
|
||||
singleton *singleton::p = nullptr;
|
||||
singleton::CGarbo Garbo;
|
||||
std::mutex singleton::lock_;
|
||||
|
||||
singleton* singleton::instance() {
|
||||
if (p == nullptr) {
|
||||
|
@ -1,5 +1,7 @@
|
||||
//
|
||||
// Created by light on 20-2-7.
|
||||
// 在C++11标准下,《Effective C++》提出了一种更优雅的单例模式实现,使用函数内的 local static 对象。
|
||||
// 这种方法也被称为Meyers' Singleton。
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user