单例模式的 模板方式实现 c++

 
  

// singleton.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "singleton.h"
int _tmain(int argc, _TCHAR* argv[])
{
	Msg::Instance().sayHello();
	return 0;
}


#ifndef SINGLETON_H
#define SINGLETON_H
#include 
template
class singleton
{
public:
	static T& Instance()
	{
	if(!pInstance)
	{
		pInstance = new T;
	}
	return *pInstance;
	}
private:
	singleton();
	static T* pInstance;
};
template T* singleton::pInstance = NULL;
class msg{
public:
	void sayHello(){std::cout<<"hello"< Msg;
#endif


你可能感兴趣的:(设计模式)