C++单例模式

#ifndef _SINGLETON_H_ #define _SINGLETON_H_

//单件类

#include <memory>

using namespace std;

template <class T> class Singleton {

public:  static T* Instance()  {   if( 0 == _instance.get())   {    _instance.reset( new T);   }   return _instance.get();  }

private:  Singleton(void){}  /*  ~Singleton(void){};  Singleton(const Singleton&){};  Singleton & operator= (const Singleton &){};*/

 static auto_ptr<T> _instance; };

template <class T> auto_ptr<T> Singleton<T>::_instance;

//实现类T必须调用以下宏

#define DECLARE_SINGLETON_CLASS(type)\ friend class auto_ptr<type>;\ friend class Singleton<type>;

#endif

 REFENCENCE: http://blog.csdn.net/liuxialong/article/details/6764688

你可能感兴趣的:(单例模式)