1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| // Singleton.h #pragma once
class Singleton { private: static Singleton* instance; Singleton() {} // 私有构造函数
public: static Singleton* getInstance() { if (instance == nullptr) { instance = new Singleton(); } return instance; }
// 禁止拷贝和赋值 Singleton(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete;
void doSomething() { // 业务逻辑 } };
// 初始化静态成员 Singleton* Singleton::instance = nullptr;
|