单例
来源:互联网 发布:配置apache服务器 编辑:程序博客网 时间:2024/04/29 20:13
#ifndef __INCLUDE_SINGLETON_H__#define __INCLUDE_SINGLETON_H__#include <iostream>#include <string.h>#include <cstdlib>using namespace std;template<typename T>class Singleton{ public: static T & GetInstance() { /*static T instance; /*局部静态对象,程序结束后自动调用析构函数*/ /*return instance;*/ Init(); return *instance_; } private: static void Init() { if ( instance_ == NULL) { instance_ = new T; atexit(Destory); } } static void Destory() { delete instance_; } Singleton() {} Singleton(const Singleton<T> &single) {} Singleton<T> & operator = (const Singleton<T> & sigle) {} ~Singleton() {} static T *instance_;};template<typename T>T* Singleton<T>::instance_ = NULL;#endif //__INCLUDE_SINGLETON_H__#include <iostream>#include "Singleton.h"using namespace std;class ApplicationImpl{ public : ApplicationImpl() { cout<<"ApplicationImpl.."<<endl; } ~ApplicationImpl() { cout<<"~ApplicationImpl.."<<endl; } void Run() { cout<<"Run.."<<endl; }};typedef Singleton<ApplicationImpl> App;int main(){ App::GetInstance().Run(); App::GetInstance().Run(); return 0;}
1 0
- 单例
- 单例
- 单例
- 单例
- 单例
- 单例
- 单例
- 单例
- 单例
- 单例
- 单例
- 单例
- 单例
- 单例
- 单例
- 单例
- 单例
- 单例
- classPath 三个文章连接
- 你确实应该学习并使用的 10 个 C# 特性
- javaw.exe是什么?两个class文件可以同事运行
- 使用补偿算法对陀螺仪和电子罗盘进行数据融合,计算方位角
- Ubuntu16.04下 Matlab2015b安装与激活及注意事项:
- 单例
- 通过IL分析C#中的委托、事件、Func、Action、Predicate之间的区别与联系
- Highcharts 相关属性
- UE4中GameInstance和GameState区别
- 数据结构实验之链表三:链表的逆置
- 5--双向链表
- HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容。
- select----2
- 子数组之和