C++笔记之单例模式

C++笔记之单例模式

参考笔记:C++笔记之call_once和once_flag

code review

文章目录

1.返回实例引用

代码

cpp 复制代码
#include <iostream>

class Singleton {
  public:
    static Singleton &getInstance() {
        static Singleton instance;
        return instance;
    }

    void printMessage() {
        std::cout << "hello world!" << std::endl;
    }

  private:
    Singleton() {}                          // 禁止外部创建实例
    Singleton(const Singleton &);           // 禁止复制实例
    Singleton operator=(const Singleton &); // 禁止赋值实例
};

int main() {
    Singleton &instance = Singleton::getInstance();
    instance.printMessage();
    return 0;
}

2.返回实例指针

代码

cpp 复制代码
#include <iostream>

class Singleton {
  public:
    static Singleton *getInstance() {
        if (instance == nullptr) {
            instance = new Singleton();
        }
        return instance;
    }

    void printMessage() {
        std::cout << "hello world!" << std::endl;
    }

  private:
    static Singleton *instance;
    Singleton() {}                          // 禁止外部创建实例
    Singleton(const Singleton &);           // 禁止复制实例
    Singleton operator=(const Singleton &); // 禁止赋值实例
};

Singleton *Singleton::instance = nullptr;

int main() {
    Singleton *instance = Singleton::getInstance();
    instance->printMessage();
    return 0;
}

3.单例和智能指针share_ptr结合

代码

cpp 复制代码
#include <iostream>
#include <memory>

class Singleton {
  public:
    static std::shared_ptr<Singleton> getInstance() {
        if (!instance) {
            instance = std::shared_ptr<Singleton>(new Singleton());
        }
        return instance;
    }

    void printMessage() {
        std::cout << "hello world!" << std::endl;
    }

  private:
    Singleton() {}                          // 禁止外部创建实例
    Singleton(const Singleton &);           // 禁止复制实例
    Singleton operator=(const Singleton &); // 禁止赋值实例

    static std::shared_ptr<Singleton> instance;
};

std::shared_ptr<Singleton> Singleton::instance = nullptr;

int main() {
    std::shared_ptr<Singleton> instance = Singleton::getInstance();
    instance->printMessage();
    return 0;
}

4.单例和std::call_once结合

编译运行:

代码

cpp 复制代码
#include <iostream>
#include <mutex>

class Singleton {
public:
    static Singleton *getInstance() {
        std::call_once(initFlag, [](){
            instance = new Singleton();
        });

        return instance;
    }

    void printMessage() {
        std::cout << "hello world!" << std::endl;
    }

private:
    static Singleton *instance;
    static std::once_flag initFlag;
    
    Singleton() {} // 禁止外部创建实例
    Singleton(const Singleton &); // 禁止复制实例
    Singleton operator=(const Singleton &); // 禁止赋值实例
};

Singleton *Singleton::instance = nullptr;
std::once_flag Singleton::initFlag;

int main() {
    Singleton *instance = Singleton::getInstance();
    instance->printMessage();
    return 0;
}

5.单例和std::call_once、unique_ptr结合

编译运行

代码

cpp 复制代码
#include <iostream>
#include <mutex>
#include <memory>

class Singleton {
public:
    static Singleton *getInstance() {
        std::call_once(initFlag, [](){
            instance.reset(new Singleton());
        });

        return instance.get();
    }

    void printMessage() {
        std::cout << "hello world!" << std::endl;
    }

private:
    static std::unique_ptr<Singleton> instance;
    static std::once_flag initFlag;

    Singleton() {} // 禁止外部创建实例
    Singleton(const Singleton &); // 禁止复制实例
    Singleton operator=(const Singleton &); // 禁止赋值实例
};

std::unique_ptr<Singleton> Singleton::instance;
std::once_flag Singleton::initFlag;

int main() {
    Singleton *instance = Singleton::getInstance();
    instance->printMessage();
    return 0;
}
相关推荐
05候补工程师2 分钟前
【线性代数笔记】初等变换、正交化与特殊矩阵性质核心总结
经验分享·笔记·线性代数·考研·矩阵
旺仔老馒头.17 分钟前
【C++】类和对象(二)
开发语言·c++·后端·类和对象
wefg126 分钟前
一些零散的算法
c++·算法
khalil102029 分钟前
代码随想录算法训练营Day-48 单调栈02 | 42. 接雨水、84.柱状图中最大的矩形
数据结构·c++·算法·leetcode·单调栈·接雨水
大大杰哥41 分钟前
leetcode hot100(3)子串
c++·算法·leetcode
Heartache boy1 小时前
野火STM32_HAL库版课程笔记-I2C介绍
笔记·stm32·单片机
莫生灬灬1 小时前
ElementUI封装 共91个组件 支持易语言/火山/C#/Python
开发语言·c++·python·ui·elementui·c#
影sir1 小时前
STL容器——vector类
c++·算法·stl
Brilliantwxx1 小时前
【C++】stack_queue与deque模版(模拟实现+认识+对比)
开发语言·c++·笔记·算法·list
一只旭宝1 小时前
【C++入门精讲13】异常处理
c++