保证一个类只有一个实例
# include <iostream>
class Singleton {
public:
static Singleton *GetSingleton() {
static Singleton obj;
return &obj;
}
private:
Singleton() {}
};
int main()
{
Singleton *obj = Singleton::GetSingleton();
return 0;
}