首页 > 编程语言 > 详细

设计模式——单例设计模式(C++实现)

时间:2017-04-20 23:02:02      阅读:285      评论:0      收藏:0      [点我收藏+]
 1 #ifndef  SINGLETONHOLDER_INC
 2 #define  SINGLETONHOLDER_INC
 3 
 4 template<class T>
 5 class SingletonHolder
 6 {
 7 public:
 8     static T* Instance()
 9     {
10         if(!pInstance_)
11             pInstance_=new T;
12         return pInstance_;
13     }
14 private:
15     SingletonHolder(){};
16     SingletonHolder(const SingletonHolder&);
17     SingletonHolder& operator=(const SingletonHolder&);
18     ~SingletonHolder(){};
19     static T* pInstance_;
20 };
21 
22 template<class T>
23 T* SingletonHolder<T>::pInstance_=0;
24 
25 
26 #endif
27 
28 
29 #ifndef INS
30 #define INS SingletonHolder<CGlobal>::Instance()
31 #endif

 结合模板和宏定义,可以很方便的单例化任何类

设计模式——单例设计模式(C++实现)

原文:http://www.cnblogs.com/070412-zwc/p/6740960.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!