单例模式-c++实现
2016-05-17 22:33
423 查看
结合前辈们的博客,写下自己写的单例模式实现,暂时没有考虑多线程情况下
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton *GetInstance()
{
return m_Instance;
}
int GetTest()
{
return m_Test;
}
private:
Singleton(){ m_Test = 10; }
static Singleton *m_Instance;
int m_Test;
// This is important
class GC
{
public :
~GC()
{
// We can destory all the resouce here, eg:db connector, file handle and so on
if (m_Instance != NULL )
{
cout<< "Here is the test" <<endl;
delete m_Instance;
m_Instance = NULL ;
}
}
};
static GC gc;
};
Singleton *Singleton ::m_Instance = new Singleton();
Singleton ::GC Singleton ::gc;
int main(int argc , char *argv [])
{
Singleton *singletonObj = Singleton ::GetInstance();
cout<<singletonObj->GetTest()<<endl;
return 0;
}
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton *GetInstance()
{
return m_Instance;
}
int GetTest()
{
return m_Test;
}
private:
Singleton(){ m_Test = 10; }
static Singleton *m_Instance;
int m_Test;
// This is important
class GC
{
public :
~GC()
{
// We can destory all the resouce here, eg:db connector, file handle and so on
if (m_Instance != NULL )
{
cout<< "Here is the test" <<endl;
delete m_Instance;
m_Instance = NULL ;
}
}
};
static GC gc;
};
Singleton *Singleton ::m_Instance = new Singleton();
Singleton ::GC Singleton ::gc;
int main(int argc , char *argv [])
{
Singleton *singletonObj = Singleton ::GetInstance();
cout<<singletonObj->GetTest()<<endl;
return 0;
}
相关文章推荐
- 库函数和系统调用
- 第8周 C语言及程序设计提高例程-31 编制自己的字符串函数
- c语言学生管理
- C语言游戏和代码
- 对Windows平台C程序内存分布研究及安全性分析
- 一起talk C栗子吧(第一百五十一回:C语言实例--socket通信地址)
- C++ string类型详解
- C++走向远洋——50(Time类中的运算符重载、一目,二目比较运算符、二目赋值运算符、二目加减法运算符)
- (未总结完成)C++中的对象放在哪里?
- C++中负数是真还是假?
- c++中vector的用法详解
- C++实现委托机制(三)——lambda表达式封装
- C++的头文件和实现文件分别写什么
- C++深层复制构造函数
- 论飞机游戏在C语言程序的基本语句能完成功能的体会
- C语言中memset函数详解
- 查询自动售货机中商品的价格
- C++多态:从虚表指针到设计模式
- C++ vector用法
- Uva 524 Prime Ring (素数环)