92、单例模式
#include <iostream>
#include <algorithm>
using namespace std;
class SingleInstance {
public:
static SingleInstance* GetInstance() {
static SingleInstance ins;
return &ins;
}
~SingleInstance(){};
private:
//涉及到创建对象的函数都设置为private
SingleInstance() { std::cout<<"SingleInstance() 饿汉"<<std::endl;
}
SingleInstance(const SingleInstance& other) {};
SingleInstance& operator=(const SingleInstance& other) {return
*this;}
};
int main(){
//因为不能创建对象所以通过静态成员函数的⽅法返回静态成员变量
SingleInstance* ins = SingleInstance::GetInstance();
return 0;
}
//输出 SingleInstance() 饿汉Last updated