C++单例模式的简单实现
2013-05-28 08:55
204 查看
一份好的代码就像 一个艺术,结构框架都很清晰,每个程序猿都希望自己的代码结构清晰,下面是简单的单例模式的实现
singleto.h
#include <iostream.h>
class Singleton
{
private:
Singleton();
public:
static Singleton *getInstance()
{
if(m_pThis == NULL)
{
m_pThis = new Singleton();
}
return m_pThis;
}
//内存释放,防止内存泄露
static void Release ()
{
if(m_pThis !=NULL)
{
delete m_pThis;
m_pThis = NULL;
}
}
//其他功能函数
void myOutPut()
{
cout<<"hello , I am Singleton"<<endl;
}
private:
static Singleton *m_pThis;
};
singleton.cpp
Singleton *Singleton::m_pThis = NULL;
Singleton::Singleton()
{
}
main.cpp
#include "singleton.h"
int main(int argc, char *argv[])
{
Singleton *s1 = Singleton::getInstance();
s1->myOutPut();
return 0;
}
singleto.h
#include <iostream.h>
class Singleton
{
private:
Singleton();
public:
static Singleton *getInstance()
{
if(m_pThis == NULL)
{
m_pThis = new Singleton();
}
return m_pThis;
}
//内存释放,防止内存泄露
static void Release ()
{
if(m_pThis !=NULL)
{
delete m_pThis;
m_pThis = NULL;
}
}
//其他功能函数
void myOutPut()
{
cout<<"hello , I am Singleton"<<endl;
}
private:
static Singleton *m_pThis;
};
singleton.cpp
Singleton *Singleton::m_pThis = NULL;
Singleton::Singleton()
{
}
main.cpp
#include "singleton.h"
int main(int argc, char *argv[])
{
Singleton *s1 = Singleton::getInstance();
s1->myOutPut();
return 0;
}
相关文章推荐
- 大话设计模式(C++实现)--简单工厂模式
- 设计模式 简单工厂 c++实现
- 《大话设计模式》读书笔记(C++代码实现) 第一章:简单工厂模式
- linux下c++实现简单的生产者消费者队列模式
- 简单工厂模式 C++实现
- C++设计模式 --单例模式(简单的实现和对象释放的控制)
- 《大话设计模式》读书笔记(C++代码实现) 第一章:简单工厂模式
- 设计模式 -- 简单工厂模式 -- c++实现
- 自己写的用c++实现的简单装饰者模式
- 【单例模式】简单C++实现
- 单例模式的C++简单实现
- 简单工厂模式-C++实现
- 大话设计模式之C++实现---简单工厂模式
- C++ 使用模版范式写一个单例模式的类--简单实现单例模式
- C++ 使用模版范式写一个单例模式的类--简单实现单例模式
- C++中单例模式的两种简单实现
- 大话设计模式c++实现之一 简单工厂模式
- 设计模式C++实现一:简单工厂模式
- C++实现单例模式的简单例程
- 使用c++简单实现reactor模式