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;
}
相关推荐
Moonquakes5405 分钟前
嵌入式学习基础笔记(51)
笔记·学习
e***98577 分钟前
C++跨平台开发的5大核心挑战与突破
开发语言·c++
橘颂TA11 分钟前
【剑斩OFFER】算法的暴力美学——leetCode 662 题:二叉树最大宽度
c++·算法·结构与算法
MSTcheng.14 分钟前
【C++】开放定址法实现哈希表!
c++·缓存·stl·散列表·哈希
玖釉-16 分钟前
[Vulkan 学习之路] 20 - 顶点缓冲区:创建顶点缓冲区 (Vertex Buffer Creation)
c++·windows·图形渲染
yi.Ist20 分钟前
博弈论 Nim游戏
c++·学习·算法·游戏·博弈论
电子小白12322 分钟前
第13期PCB layout工程师初级培训-5-生产制造
笔记·嵌入式硬件·学习·制造·pcb·layout
yuanmenghao22 分钟前
车载Linux 系统问题定位方法论与实战系列 - 系统 reset / reboot 问题定位
linux·服务器·数据结构·c++·自动驾驶
楼田莉子24 分钟前
C++高级数据结构——LRU Cache
数据结构·c++·后端·学习
DYS_房东的猫25 分钟前
macOS 上 C++ 开发完整指南(2026 年版)
开发语言·c++·macos