item2,实现singleton模式
2016-06-23 20:52
357 查看
单例模式?
只能实现一个实例的类成为单例。
==============
muduo库中单例模式实现
只能实现一个实例的类成为单例。
==============
muduo库中单例模式实现
#include<boost/noncopyable.hpp> //#include "../sort.h" //#include "getLeastNumber.h" #include<set> using namespace std; template<typename T> class Singleton:boost::noncopyable{ public: static T& instance(){ pthread_once(&ponce_,&Singleton::init); return *value_; } private: Singleton(); ~Singleton(); static void init(){ value_ = new T(); } private: static pthread_once_t ponce_; static T* value_; }; template<typename T> pthread_once_t Singleton<T>::ponce_ = PTHREAD_ONCE_INIT; template<typename T> T* Singleton<T>::value_ = nullptr; class Foo{ public: Foo(){}; ~Foo(){}; void show(){std::cout<<"begin"<<"\nend"<<std::endl;} }; int main() { //A bfs; //bfs.test(); Foo& foo = Singleton<Foo>::instance(); foo.show(); return 0; }
相关文章推荐
- hdu 2068 RPG的错排
- Java 4种方法实现选择排序算法
- Android SDK下载速度慢的解决方法(简单使用代理)
- HDU1257 最少拦截系统 贪心算法 (多人乘船问题)水题
- Android Studio项目目录结构介绍
- HTTP请求方法
- 数据库备份checksum选项你会用么?
- ubuntu启动无桌面图标?
- [HihoCoder]#1014 : Trie树
- Android命令行创建并运行 AVD模拟器
- Problem4-1001
- c++从int转string、char的方法总结
- hdu2084 数塔 记忆化搜索水题
- C++STL之vector
- 安卓开发中非常炫的效果集合
- HDU 3530 单调队列
- HDU 1003Max Sum 动态规划 经典题 最大子序列和
- 神经网络和深度学习简史(四):深度学习终迎伟大复兴
- HDU 1231 最大连续子序列
- APP测试点总结(功能,交互,死机崩溃状态分析,容易出错的检查点)