1. 程式人生 > >Design Patterns 5: Singleton

Design Patterns 5: Singleton

確保一個類(Singleton)只有一個例項(_instance)

struct Singleton
{
	static Singleton* Instance()
	{
		return _instance ? _instance : new Singleton;
	}
protected:
	Singleton() = default;
private:
	static Singleton * _instance;
};

Singleton* Singleton::_instance = nullptr;

int main()
{
	auto p = Singleton::Instance();
}