//单例类的C++实现
#include <iostream>
using namespace std;
class Singleton
{
private:
Singleton(){} //注意这里是私有构造函数.所以不能实例化.
public:
static Singleton& GetInstance() {
static Singleton singleton;
return singleton;
}
void foo(){
std::cout<<"foo"<<std::endl;
}
};
int main()
{
Singleton::GetInstance().foo();
Singleton* p1 = &Singleton::GetInstance();
Singleton* p2 = &Singleton::GetInstance();
//p1,p2指向同一地址
return 0;
}