In C++, the only code that guaranteed to be executed after an exception is thrown are the destructors of objects residing in stack and that's why we need RAII. We don't always deal with head based objects, so instead with using auto_ptrs and shared_ptrs, sometimes we need to create our own resource managment class. And here are some rules you should follow:

For coping constructor and coping assignment operator

1. prohibit copying. If copying an RAII object makes no sense, you should explicitly prohibit it. You can either declare copy constructor private or inherit from Uncopyable class (see Item 6)

2. Reference-count the underlying resources. Sometimes it's desirable to hold on the resource until the last object using it is destroyed. When that's the case, copying an RAII object should increse the count of the number of referring to that object. That is how shared_ptr implement copying behavior. So RAII can implement reference-counting copying by containing a shared_ptr data member. In addition, if you don't want to delete the resource when the reference count goes to 0 which is the default behavior of shared_ptr, you can provide a customized "deleter" to shared_ptr which will be called when reference count goes to 0.

class Lock{
public:
explicit Lock(Mutex *pm)
: mutexPtr(pm, unlock) {
lock(mutexPtr.get());
} private:
std::tr1::shared_ptr<Mutex> mutexPtr;
};

  

3. copy the underlying resources

In this case, the only reason to use recource-managing class is to make sure that resource would be released after you done with it. You need to make a deep copy of underlying resource

4. Transfer the ownership of underlying resources

Sometimes, you want to keep only one RAII object refers to raw resource, you need to transfer the ownership to new instance while copying. This kind of behavior is implemented by auto_ptr

Effective C++ Item 14 Think carefully about copying behavior in resource-managing classe的更多相关文章

  1. 读书笔记 effective c++ Item 14 对资源管理类的拷贝行为要谨慎

    1. 自己实现一个资源管理类 Item 13中介绍了 “资源获取之时也是初始化之时(RAII)”的概念,这个概念被当作资源管理类的“脊柱“,也描述了auto_ptr和tr1::shared_ptr是如 ...

  2. 条款14:在资源管理类中心copying行为(Think carefully about copying behavior in resource-manage classes)

    NOTE: 1.复制RAII 对象必须一并赋值它所管理的资源,所以资源的copying行为决定RAII对象的copying行为. 2.普遍而常见的RAII class copying 行为是: 抑制c ...

  3. 读书笔记 effective c++ Item 15 在资源管理类中提供对原生(raw)资源的访问

    1.为什么需要访问资源管理类中的原生资源  资源管理类是很奇妙的.它们是防止资源泄漏的堡垒,没有资源泄漏发生是设计良好的系统的一个基本特征.在一个完美的世界中,你需要依赖这样的类来同资源进行交互,绝不 ...

  4. 读书笔记 effective c++ Item 49 理解new-handler的行为

    1. new-handler介绍 当操作符new不能满足内存分配请求的时候,它就会抛出异常.很久之前,它会返回一个null指针,一些旧的编译器仍然会这么做.你仍然会看到这种旧行为,但是我会把关于它的讨 ...

  5. 读书笔记 effective c++ Item 18 使接口容易被正确使用,不容易被误用

    1. 什么样的接口才是好的接口 C++中充斥着接口:函数接口,类接口,模板接口.每个接口都是客户同你的代码进行交互的一种方法.假设你正在面对的是一些“讲道理”的人员,这些客户尝试把工作做好,他们希望能 ...

  6. 读书笔记 effective c++ Item 29 为异常安全的代码而努力

    异常安全在某种意义上来说就像怀孕...但是稍微想一想.在没有求婚之前我们不能真正的讨论生殖问题. 假设我们有一个表示GUI菜单的类,这个GUI菜单有背景图片.这个类将被使用在多线程环境中,所以需要mu ...

  7. Effective C++ Item 13 Use object to manage resources

    1. Always use object to manage resource! If you delete a pointer or release a handler manually by yo ...

  8. Effective C++ -----条款14: 在资源管理类中小心copying行为

    复制RAII对象必须一并复制它所管理的资源,所以资源的copying行为决定RAII对象的copying行为. 普遍而常见的RAII class copying行为是:抑制copying(使用私有继承 ...

  9. Effective C++学习笔记(Part One:Item 1-4)

    最近的最终effectvie C++仔细阅读侧,我很惊讶C++动力和魅力.最近的" LL最近记得阅读体验和读书笔记其.必要查找使用,是什么假设总结不合适.欢迎批评: 如今仅仅列出框架,近期会 ...

随机推荐

  1. 原始tab栏切换

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. mycli---数据库工具(提示、自动补全)

    前言 朋友介绍了一个工具,mycli,支持MySQL查询语句自动补全等,这里给大家介绍一下. 大家也可以直接去官网看一下,安装使用都很简单. 安装 $ pip install -U mycli $ b ...

  3. Web攻防系列教程之文件上传攻防解析(转载)

    Web攻防系列教程之文件上传攻防解析: 文件上传是WEB应用很常见的一种功能,本身是一项正常的业务需求,不存在什么问题.但如果在上传时没有对文件进行正确处理,则很可能会发生安全问题.本文将对文件上传的 ...

  4. SharePoint自动化系列——通过PowerShell创建SharePoint Site Collection

    通过PowerShell创建SharePoint Site Collection,代码如下: Add-PSSnapin microsoft.sharepoint.powershell function ...

  5. 在django中访问静态文件(js css img)

    刚开始参考的是别的文章,后来参考文章<各种 django 静态文件的配置总结>才看到原来没有但是没有注意到版本,折腾了一晚上,浪费了很多很多时间.后来终于知道搜索django1.7访问静态 ...

  6. 深入浅出c++之---this指针

    前言:C语言中的数组指针和指针数组 数组指针,是指向数组的指针的缩写:指针数组,是存放指针的数组的缩写.其实很多时候,往往因为简写和缩写带给我们很多困惑.我曾想过不用简称去学习,但在很多时候,我们查询 ...

  7. pyqt重写键盘事件+获取信号发送对象

    # _*_ coding:utf-8 _*_ import sys from PyQt4 import QtGui,QtCore class Example(QtGui.QMainWindow): d ...

  8. JavaScript高级 面向对象(8)--浅拷贝代码实现

    说明(2017.3.31): 1. 浅拷贝,只有值属性,没有引用属性. 2. 在原对象里面添加一个copy方法,返回本对象内的所有值属性. <!DOCTYPE html> <html ...

  9. ios UITextField文本框基本使用,以及所有代理方法的作用

    /* UITextField文本输入框 */ UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 50 ...

  10. 【WPF】使用Popup控件做浮窗/提示框

    需求:当鼠标移入某个区域时,弹出一个浮窗,以便用户进行下一步操作. 效果如下图: 当鼠标移入左上角的[多选显示]框内,出现下面的浮窗(悬浮在原UI之上).当在浮窗外点击鼠标左键时,隐藏该浮窗. 由于该 ...