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

原文链接: https://www.cnblogs.com/cosophy/archive/2012/04/23/2466684.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++单例模式

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/48306

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午12:08
下一篇 2023年2月9日 上午12:09

相关推荐