C++笔记之单例通过GetInstance传递参数

C++笔记之单例通过GetInstance传递参数

code review!

例1.普通指针的单例

运行

代码

#include <iostream>
#include <string>

class Singleton {
          
   
  public:
    // 获取单例实例的静态函数,可以传递构造函数所需的参数
    static Singleton *GetInstance(const std::string &value) {
          
   
        if (!instance) {
          
   
            instance = new Singleton(value);
        }
        return instance;
    }

    void PrintValue() {
          
   
        std::cout << value << std::endl;
    }

  private:
    // 私有构造函数,防止直接实例化
    Singleton(const std::string &val) : value(val) {
          
   }

    static Singleton *instance;
    std::string value;
};

// 初始化静态成员变量
Singleton *Singleton::instance = nullptr;

int main() {
          
   
    Singleton *instance1 = Singleton::GetInstance("Instance 1");
    instance1->PrintValue(); // 输出:Instance 1

    Singleton *instance2 = Singleton::GetInstance("Instance 2");
    instance2->PrintValue(); // 输出:Instance 1,因为单例已经创建,不会再次创建新实例

    return 0;
}

例2.结合智能指针和std::call_once

使用 std::shared_ptr 和 std::call_once 来实现单例模式,并避免了调用私有构造函数: 在这个示例中,通过直接使用 new Singleton(value) 来创建 std::shared_ptr<Singleton> 的实例,这样就能够调用私有的构造函数。

运行

代码

#include <iostream>
#include <memory>
#include <string>
#include <mutex>

class Singleton {
          
   
public:
    // 获取单例实例的静态函数,可以传递构造函数所需的参数
    static std::shared_ptr<Singleton> GetInstance(const std::string& value) {
          
   
        std::call_once(initFlag, [&]() {
          
   
            instance = std::shared_ptr<Singleton>(new Singleton(value));
        });
        return instance;
    }

    void PrintValue() {
          
   
        std::cout << value << std::endl;
    }

private:
    // 私有构造函数,防止直接实例化
    Singleton(const std::string& val) : value(val) {
          
   }

    static std::shared_ptr<Singleton> instance;
    static std::once_flag initFlag;
    std::string value;
};

// 初始化静态成员变量
std::shared_ptr<Singleton> Singleton::instance = nullptr;
std::once_flag Singleton::initFlag;

int main() {
          
   
    std::shared_ptr<Singleton> instance1 = Singleton::GetInstance("Instance 1");
    instance1->PrintValue(); // 输出:Instance 1

    std::shared_ptr<Singleton> instance2 = Singleton::GetInstance("Instance 2");
    instance2->PrintValue(); // 输出:Instance 1,因为单例已经创建,不会再次创建新实例

    return 0;
}

例3.编译不通过的错误例子,在GetInstance内不可以使用std::make_shared来创建对象

编译报错:

在 C++ 中,std::make_shared 会调用构造函数来创建对象,但是由于构造函数是私有的,会导致编译错误。

经验分享 程序员 微信小程序 职场和发展