#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;
}
前三条cout语句是期待打印同样的地址,为何每次会不一样呢?