1.懒汉式
class Singleton{
private:
Singleton(){}
private:
static *Sinleton instance;
static *Garbo garbo;
public:
static *Singleton::getInstance(){
if(instance=nullptr){
instance = new Sinleton();
}
return instance;
}
class Garbo{
~Garbo(){
if(instance!=nullptr){
delete instance;
}
}
}
};
Sinleton* Sinleton::instance = nullptr;
懒汉式:使用时才实例化对象,单线程没问题,多线程不可靠
2.饿汉式:
class Singleton{
private:
Singleton(){}
private:
static *Sigleton instance;
public:
static *Sinleton::getInstance(){
return instance;
}
};
Sinleton *Singleton::instance = new Singleton();