#include <iostream>
#include <vector>
using namespace std;
class Singleton {
public:
static Singleton* getInstance() {
if (instance == nullptr) {
instance = new Singleton();
}
return instance;
}
~Singleton() {
if (instance) {
delete instance;
}
}
//构造函数、拷贝构造、拷贝赋值、移动构造、移动赋值函数设为私有
private:
Singleton() {};
Singleton(const Singleton& obj) = delete;
Singleton operator=(const Singleton& obj) = delete;
Singleton(const Singleton&& obj) = delete;
Singleton operator=(const Singleton&& obj) = delete;
static Singleton* instance;
};
Singleton* Singleton::instance = nullptr;
int main() {
Singleton::getInstance();
}
C++单例设计模式 singleton
于 2023-04-06 18:14:14 首次发布