网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C/C++
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.C/C++中的日期和时间.
.C数值计算程序移植到VC开发环境.
.DOS界面下通用图形编辑软件的设计.
.指针与指针变量.
.More Effective C++:避免缺省构.
.C++对象布局及多态探索之菱形结构.
.基于Nokia S60的游戏开发之四.
.C++中确定基类有虚析构函数.
.C++Builder动态更改自定义打印纸.
.回文数的形成.
.永远的C++,永远的追求.
.Raw Socket(原始套接字)实现Sn.
.在代码隐藏中遍历当前页的所有控.
.C++ Builder 初学问与答(十二).
.制作主窗口显示前的版权窗口.
.C++基本概念在编译器中的实现.
.MCI(媒体控制接口)相关知识.
.具体而微的绘图程式-c++ Borland.
.C++数据结构学习:二叉树(2).
.自定义快速报表的打印预览窗口.

C++中的Singleton类的实现(1)

发表日期:2008-3-8


《设计模式》中把 Singleton 写成返回指针:




class Singleton{

public:

static Singleton* Instance();

protected:

Singleton();

private:

static Singleton* _instance;

}; 相应的实现 cpp 文件是:

Singleton* Singleton::_instance;

Singleton* Singleton::Instance(){

if( _instance == 0){

_instance = new Singleton;

};

return _instance;

} 将构造函数设计成 protected 的目的是防止在 class 外面 new ,有人可能会设计成 private ,假如考虑到有可能会继续这个类的话,还是将构造函数设计成 protected 比较好,还需要加一个 virtual 析构函数。为了防止别人复制 Singleton 对象:

Singleton* pSingleton = Singleton::Instance();

Singleton s1 = *pSingleton;

Singleton s2 = *pSingleton;

需要将拷贝构造(copy constrUCtor)函数变成 private。

但是这里存在的问题是,什么时候删除 Singleton 对象?按照 C++ 的一个基本原则,对象在哪里创建就在哪里销毁,这里还应该放一个 destroy 方法来删除 Singleton 对象。假如忘了删除就比较麻烦。Instance 函数还存在多线程同时访问的加锁问题。假如把 Instance 函数开始和结尾放上加锁和解锁,整个函数性能会下降很多。这不是一个好的设计。

有一个小小的改动,可以避免忘了删除 Singleton 对象带来内存泄露的问题。那就是用 std:auto_ptr 来包含 Singleton 对象,定义一个class static member auto_ptr 对象,在析构的静态 auto_ptr 变量的时候时候自动删除 Singleton 对象。为了不让用户 delete Singleton 对象,需要将析构函数由 public 变成 protected。以下是头文件 SingletonAutoPtr.h :

#include <memory>

using namespace std;

class CSingletonAutoPtr

{

private:

static auto_ptr<CSingletonAutoPtr> m_auto_ptr;

static CSingletonAutoPtr* m_instance;

protected:

CSingletonAutoPtr();

CSingletonAutoPtr(const CSingletonAutoPtr&);

virtual ~CSingletonAutoPtr();

//allow auto_ptr to delete, using protected ~CSingletonAutoPtr()

friend class auto_ptr<CSingletonAutoPtr>;

public:

static CSingletonAutoPtr* GetInstance();

void Test();

}; 《设计模式》中把 Singleton 写成返回指针:




class Singleton{

public:

static Singleton* Instance();

protected:

Singleton();

private:

static Singleton* _instance;

}; 相应的实现 cpp 文件是:

Singleton* Singleton::_instance;

Singleton* Singleton::Instance(){

if( _instance == 0){

_instance = new Singleton;

};

return _instance;

} 将构造函数设计成 protected 的目的是防止在 class 外面 new ,有人可能会设计成 private ,假如考虑到有可能会继续这个类的话,还是将构造函数设计成 protected 比较好,还需要加一个 virtual 析构函数。为了防止别人复制 Singleton 对象:

Singleton* pSingleton = Singleton::Instance();

Singleton s1 = *pSingleton;

Singleton s2 = *pSingleton;

需要将拷贝构造(copy constructor)函数变成 private。


但是这里存在的问题是,什么时候删除 Singleton 对象?按照 C++ 的一个基本原则,对象在哪里创建就在哪里销毁,这里还应该放一个 destroy 方法来删除 Singleton 对象。假如忘了删除就比较麻烦。Instance 函数还存在多线程同时访问的加锁问题。假如把 Instance 函数开始和结尾放上加锁和解锁,整个函数性能会下降很多。这不是一个好的设计。

有一个小小的改动,可以避免忘了删除 Singleton 对象带来内存泄露的问题。那就是用 std:auto_ptr 来包含 Singleton 对象,定义一个class static member auto_ptr 对象,在析构的静态 auto_ptr 变量的时候时候自动删除 Singleton 对象。为了不让用户 delete Singleton 对象,需要将析构函数由 public 变成 protected。以下是头文件 SingletonAutoPtr.h :

#include <memory>

using namespace std;

class CSingletonAutoPtr

{

private:

static auto_ptr<CSingletonAutoPtr> m_auto_ptr;

static CSingletonAutoPtr* m_instance;

protected:

CSingletonAutoPtr();

CSingletonAutoPtr(const CSingletonAutoPtr&);

virtual ~CSingletonAutoPtr();

//allow auto_ptr to delete, using protected ~CSingletonAutoPtr()

friend class auto_ptr<CSingletonAutoPtr>;

public:

static CSingletonAutoPtr* GetInstance();

void Test();

}; QQread.com 推出游戏功略 http://www.qqread.com/netgame/game/index.Html 魔兽世界 跑跑卡丁车 街头篮球 水浒Q传 龙与地下城OL 征服  轩辕剑5 FIFA07 热血江湖 大唐风云 梦幻西游 武林外传 《设计模式》中把 Singleton 写成返回指针:




class Singleton{

public:

static Singleton* Instance();

protected:

Singleton();

private:

static Singleton* _instance;

}; 相应的实现 cpp 文件是:

Singleton* Singleton::_instance;

Singleton* Singleton::Instance(){

if( _instance == 0){

_instance = new Singleton;

};

return _instance;

}
将构造函数设计成 protected 的目的是防止在 class 外面 new ,有人可能会设计成 private ,假如考虑到有可能会继续这个类的话,还是将构造函数设计成 protected 比较好,还需要加一个 virtual 析构函数。为了防止别人复制 Singleton 对象:

Singleton* pSingleton = Singleton::Instance();

Singleton s1 = *pSingleton;

Singleton s2 = *pSingleton;

需要将拷贝构造(copy constructor)函数变成 private。

但是这里存在的问题是,什么时候删除 Singleton 对象?按照 C++ 的一个基本原则,对象在哪里创建就在哪里销毁,这里还应该放一个 destroy 方法来删除 Singleton 对象。假如忘了删除就比较麻烦。Instance 函数还存在多线程同时访问的加锁问题。假如把 Instance 函数开始和结尾放上加锁和解锁,整个函数性能会下降很多。这不是一个好的设计。

有一个小小的改动,可以避免忘了删除 Singleton 对象带来内存泄露的问题。那就是用 std:auto_ptr 来包含 Singleton 对象,定义一个class static member auto_ptr 对象,在析构的静态 auto_ptr 变量的时候时候自动删除 Singleton 对象。为了不让用户 delete Singleton 对象,需要将析构函数由 public 变成 protected。以下是头文件 SingletonAutoPtr.h :

#include <memory>

using namespace std;

class CSingletonAutoPtr

{

private:

static auto_ptr<CSingletonAutoPtr> m_auto_ptr;

static CSingletonAutoPtr* m_instance;

protected:

CSingletonAutoPtr();

CSingletonAutoPtr(const CSingletonAutoPtr&);

virtual ~CSingletonAutoPtr();

//allow auto_ptr to delete, using protected ~CSingletonAutoPtr()

friend class auto_ptr<CSingletonAutoPtr>;

public:

static CSingletonAutoPtr* GetInstance();

void Test();

}; QQread.com 推出游戏功略 http://www.qqread.com/netgame/game/index.html 魔兽世界 跑跑卡丁车 街头篮球 水浒Q传 龙与地下城OL 征服  轩辕剑5 FIFA07 热血江湖 大唐风云 梦幻西游 武林外传
上一篇:C++箴言:最小化文件之间的编译依赖 人气:807
下一篇:VC中用于调试程序的几个宏的使用技巧 人气:587
浏览全部C/C++的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐