单例模式的 模板方式实现 c++
2015-09-18 19:28
525 查看
</pre><pre>
// singleton.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "singleton.h" int _tmain(int argc, _TCHAR* argv[]) { Msg::Instance().sayHello(); return 0; }
#ifndef SINGLETON_H #define SINGLETON_H #include <iostream> template<class T> class singleton { public: static T& Instance() { if(!pInstance) { pInstance = new T; } return *pInstance; } private: singleton(); static T* pInstance; }; template<class T> T* singleton<T>::pInstance = NULL; class msg{ public: void sayHello(){std::cout<<"hello"<<std::endl;}; }; typedef singleton<msg> Msg; #endif
相关文章推荐
- C++指针
- iOS学习笔记之-C语言基础02(关键字、标识符、注释、数据类型)
- C语言编程中碰到一个问题,求大神指教
- 学习ios第八天 番外篇 《c语言小游戏 推箱子的实现》
- 1.5 如何创建C++程序
- C++中map按value排序
- 1.4 C++程序的编写和实现
- iOS学习笔记之-C语言基础01
- 1.3 C++程序的构成和书写形式
- C语言中函数声明实现的位置
- 1.2 第一个C++程序
- [C++]unordered_map的使用
- 1.1 从C到C++,C语言与C++的关系
- C++技术点积累(5)——泛型编程(函数模板、类模板)
- c++没有实例化的类成员访问
- c++生成随机数
- 【C++】智能指针
- C++STL 迭代器
- 用 C++实现的简单物流信息存储
- Effective C++——operator =