64,666
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class Singleton {
public:
static Singleton * GetInstance() { return _pInstance == 0 ? (new Singleton) : _pInstance;}
private:
Singleton() {}
~Singleton() {delete _pInstance;}
private:
static Singleton * _pInstance ;
};
Singleton * Singleton::_pInstance = 0;
int main(void)
{
cout<<"The point of instance is " <<hex <<Singleton::GetInstance() <<endl;
cout<<"The point of instance is " <<hex <<Singleton::GetInstance() <<endl;
cout<<"The point of instance is " <<hex <<Singleton::GetInstance() <<endl;
cout<<"Size of Singleton is " <<hex <<sizeof(Singleton) <<endl; // 1
return 0;
}
static Singleton * GetInstance() {
return _pInstance == 0 ? (_pInstance = new Singleton) : _pInstance;
}