单例模式:
#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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)