c++单例模式

在C++中,单例模式是一种创建对象的设计模式,该模式限制了一个类只能有一个实例,并提供了一个全局访问点来获取该实例。当你想确保一个类只有一个实例,并且提供一个全局访问点时,这种模式就很有用。

然而,直接使用模板来实现单例模式可能并不是最直接或最常见的方法,因为单例模式通常是为特定的类设计的。但是,你可以通过模板参数化单例模式来创建一个通用的单例类模板,这样你就可以为任何类创建单例了。

以下是一个使用模板实现的单例模式的示例:

 

cpp复制代码

#include <iostream>
#include <mutex>
template<typename T>
class Singleton {
private:
static T* instance;
static std::mutex mtx; // 用于线程安全的互斥锁
Singleton() = default; // 私有构造函数,防止外部直接创建实例
Singleton(const Singleton&) = delete; // 禁止拷贝构造
Singleton& operator=(const Singleton&) = delete; // 禁止赋值操作
public:
static T& getInstance() {
std::lock_guard<std::mutex> lock(mtx); // 锁定互斥锁
if (!instance) {
instance = new T(); // 懒汉式实现,只在第一次需要时创建实例
}
return *instance;
}
// 如果你的单例需要被销毁(比如使用了动态分配的资源),可以提供一个静态的销毁函数
static void destroyInstance() {
std::lock_guard<std::mutex> lock(mtx); // 锁定互斥锁
if (instance) {
delete instance;
instance = nullptr;
}
}
// 注意:如果你的单例类有虚函数,你可能还需要考虑析构函数的虚函数性质和类的继承关系
};
template<typename T>
T* Singleton<T>::instance = nullptr;
template<typename T>
std::mutex Singleton<T>::mtx;
// 使用示例
class MyClass {
public:
MyClass() { std::cout << "MyClass instance created\n"; }
~MyClass() { std::cout << "MyClass instance destroyed\n"; }
void doSomething() { std::cout << "Doing something...\n"; }
};
int main() {
MyClass& instance1 = Singleton<MyClass>::getInstance();
MyClass& instance2 = Singleton<MyClass>::getInstance();
// 验证两个引用是否指向同一个对象
if (&instance1 == &instance2) {
std::cout << "Both instances point to the same object\n";
}
instance1.doSomething();
// 如果需要,可以销毁单例实例(但通常不推荐,除非你有明确的理由)
// Singleton<MyClass>::destroyInstance();
return 0;
}

注意:这个示例使用了懒汉式实现,这意味着单例实例是在第一次调用 getInstance() 时创建的。同时,为了线程安全,我添加了一个互斥锁来保护实例的创建。但是,在多线程环境中,更常见的做法是使用更高效的线程安全实现,如饿汉式(在类定义时即创建实例)或使用C++11的std::call_once

此外,请注意,在某些情况下,你可能不希望或不需要销毁单例实例。在上面的示例中,我提供了一个 destroyInstance() 函数作为选项,但通常不推荐使用它,除非你有明确的理由需要销毁单例实例。

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值