单例模式:
#include <iostream>
using namespace std;
class Singleton
{
public:
~Singleton() {
cout << "~Singleton虚构函数" << endl;
}
Singleton(const Singleton&) = delete;//禁止使用该函数,防止使用错误
Singleton& operator=(const Singleton&) = delete;//禁止使用该函数,防止使用错误
static Singleton& getinstance(){
static Singleton singleton;
cout << "Singleton getinstance" << endl;
return singleton;
}
void test() = delete;
void Run() {
cout << "Run 运行" << endl;
}
private:
Singleton() {
cout << "Singleton构造函数" << endl;
}
};
使用:
Singleton &singleton1 = Singleton::getinstance();
Singleton &singleton2 = Singleton::getinstance();
singleton1.Run();