//懒汉式
include [HTML_REMOVED]
using namespace std;
class Singleton{
private:
static Singleton* sg;
public:
static Singleton* getInstance(){
if(sg == nullptr){
sg = new Singleton;
}
return sg;
}
};
Singleton* Singleton:: sg = nullptr;
int main(){
Singleton* a = Singleton::getInstance();
Singleton* b = Singleton::getInstance();
cout << a << " " << b << " " << endl;
return 0;
}
//饿汉式
include [HTML_REMOVED]
using namespace std;
class Singleton{
private:
Singleton(){};
private: static Singleton* sg;
public:
static Singleton* getInstance(){
return sg;
}
};
Singleton* Singleton:: sg = new Singleton();
int main(){
Singleton* a = Singleton::getInstance();
Singleton* b = Singleton::getInstance();
cout << a << " " << b << " " << endl;
return 0;
}