懒汉模式
#include<iostream>
using namespace std;
class singleton {
private://赋值,拷贝,构造,自身指针
singleton& operator=(singleton&) = delete;
singleton(singleton&) = delete;
singleton()
{
cout << "constructor called!" << endl;
}
static singleton * ptr;
public://析构,定义指针ptr
~singleton()
{
cout<< "destructor called!" << endl;
}
static singleton* get()
{
if (ptr == nullptr)
ptr = new singleton;
return ptr;
}
};
singleton* singleton::ptr = nullptr;
int main()
{
singleton*a=singleton::get();
singleton*b = singleton::get();
return 0;
}