c++单例模式

发布于:2024-05-20 ⋅ 阅读:(133) ⋅ 点赞:(0)

在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() 函数作为选项,但通常不推荐使用它,除非你有明确的理由需要销毁单例实例。


网站公告

今日签到

点亮在社区的每一天
去签到