C++的单例模式
2013-05-11 16:04
155 查看
Student.h
[/code]
[/code]
定义另外一个测试类:SingletonTest.h
[/code]
[/code]
调用测试:SingletonTest::Instance()->test();
[code][code]
#ifndef __SINGLETON_H__
#define __SINGLETON_H__
#include <memory>
template<class T>
class SingleT
{
public:
static T * Instance()
{
if (!p)
{
p = new T;
}
return p;
}
static void Create()
{
if (!p)
{
p = new T;
}
}
static void Destroy()
{
if (p)
{
delete p;
p = NULL;
}
}
static T * Get()
{
return p;
}
static void Reset()
{
Destroy();
Create();
}
protected:
static T * p;
};
template <class T>
T * SingleT<T>::p = NULL;
#endif
[/code]
[/code]
定义另外一个测试类:SingletonTest.h
[code]
[code]
#ifndef __SINGLETONTEST_H__
#define __SINGLETONTEST_H__
#include "Singleton.h"
#include <iostream>
class SingletonTest : public SingleT<SingletonTest>
{
public:
SingletonTest(){};
~SingletonTest(){};
public:
inline void test(){std::cout<<"test()"<<std::endl;}
};
#endif
[/code]
[/code]
调用测试:SingletonTest::Instance()->test();
相关文章推荐
- C++设计模式——单例模式
- C++的三种单例模式-----深度解析
- 三种工厂模式的C++实现 推荐
- C++设计模式开放封闭原则
- 宏定义C++单例模式
- 设计模式系列7-----C++实现访问者模式(Visitor Pattern)
- c++装饰模式
- [GoF设计模式]Flyweight模式和State模式的C++实现
- 设计模式C++学习笔记之一(Strategy策略模式)
- c++控制台输入密码模式
- C++设计模式-Observer观察者模式
- C++设计模式-AbstractFactory抽象工厂模式
- 设计模式C++实现十六:迭代器模式
- 【转帖】使用 C++ 编写内核模式驱动程序的优点与缺点
- C++设计模式-适配器(转)
- C++ 单例模式,
- 应用广泛的C++单例模式
- 我所理解的设计模式(C++实现)——迭代器模式(Iterator Pattern)
- C++设计模式从0进击-1-简单(静态)工厂模式
- 单例模式与全局唯一id的思考----c++ ,c ,python 实现