c++单例模式

#include 
#include 

class Singleton 
{
private:
	Singleton();	
public:
	virtual ~Singleton();
	static Singleton *GetInstance();

private:
	static Singleton *m_pInstance;
};

Singleton *Singleton::m_pInstance = NULL;

Singleton::Singleton()
{
	std::cout << "class Singleton - Constructor" << std::endl;
}

Singleton::~Singleton()
{
	delete m_pInstance;
}

Singleton *Singleton::GetInstance()
{
	if (m_pInstance == NULL)
	{
		m_pInstance = new Singleton();
	}

	return m_pInstance;
}


int main()
{
	Singleton *Obj1 = Singleton::GetInstance();
	Singleton *Obj2 = Singleton::GetInstance();

	system("pause");
	return 0;
}




你可能感兴趣的:(c)