1. Always use object to manage resource! If you delete a pointer or release a handler manually by yourself, there is great chance that you will make mistake or forget something.

2. There are two critical aspects of using object to manage resources:

  2.1 Resources are acquired and immediately turned over to resource-managing objects. The idea of using object to manage resource is often called   Resource Acquisition is Initialization (RAII)

  2.2 Resource-managing objects use their destructors to ensure that resources are released

3. You can use std::auto_ptrs or std::tr1::shared_ptr to manage heap memory. And there are several points you should keep in mind when you using those two smart pointers.

  3.1 std::auto_ptrs assumes solo ownership which means copying one to another will set the privious one to null.

  3.2 STL contains require that their contents exhibit "normal" copying behavior, so containers of std::auto_ptrs are not allowed.

  3.3 Both std::auto_ptrs and std::tr1::shared_ptrs use delete in their destructor, not delete []. So using them to manage dynamic allocated memory is not good idea, though, regrettably, one that will compile.

Effective C++ Item 13 Use object to manage resources的更多相关文章

  1. effective c++ 条款13 use object to manage resources.

    请求的系统资源需要最终还回系统,为了避免遗忘返还这个动作,可以利用析构函数在object销毁时自动调用的特点来实现. 简单说就是用object来管理资源. 以内存资源为例 class Investme ...

  2. Effective C++ Item 15 Provide access to raw resources in resource-managing classes

    In last two item, I talk about resource-managing using RAII, now comes to the practical part. Often, ...

  3. 读书笔记 effective c++ Item 13 用对象来管理资源

    1.不要手动释放从函数返回的堆资源 假设你正在处理一个模拟Investment的程序库,不同的Investmetn类型从Investment基类继承而来, class Investment { ... ...

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

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

  5. 读书笔记 effective c++ Item 17 使用单独语句将new出来的对象放入智能指针

    1. 可能会出现资源泄漏的一种用法 假设我们有一个获取进程优先权的函数,还有一个在动态分类的Widget对象上根据进程优先权进行一些操作的函数: int priority(); void proces ...

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

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

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

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

  8. 读书笔记 effective c++ Item 31 把文件之间的编译依赖降到最低

    1. 牵一发而动全身 现在开始进入你的C++程序,你对你的类实现做了一个很小的改动.注意,不是接口,只是实现:一个私有的stuff.然后你需要rebuild你的程序,计算着这个build应该几秒钟就足 ...

  9. 读书笔记 effective c++ Item 54 让你自己熟悉包括TR1在内的标准库

    1. C++0x的历史渊源 C++标准——也就是定义语言的文档和程序库——在1998被批准.在2003年,一个小的“修复bug”版本被发布.然而标准委员会仍然在继续他们的工作,一个“2.0版本”的C+ ...

随机推荐

  1. MFC图形编辑器

    前言 vs2015竟然可以完美打开工程,哈哈可以直接生成类图了.由于内容较多,所以根据内容的重要性会安排详略. https://github.com/bajdcc/GraphEditor/releas ...

  2. 影梭Android版使用教程

    影梭Android版使用教程  2015年5月13日  admin  影梭使用教程 下载影梭Android版客户端 安卓客户端下载:下载地址 安装并打开影梭 按下图说明设置服务器.远程端口.密码和加密 ...

  3. silverlight调用WebService传递json接收绑定数据

    1.接收数据: WebService通过接口接收数据.类型为object 2.类型转换: 通过json转换方法将object转换为json格式数据 3.调用WebService方法: silverli ...

  4. 从sql中image类型字段中导出图片

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  5. 3D跑马灯效果

    睡了13个小时,发烧终于退了,持续2周的感冒看起来终于好了点,这一周一直在看perspective的一些资料,写一个3D跑马灯的效果. 个人感觉主要就是理解视角的概念,也就是perspective和p ...

  6. C语言 · 完美的代价

    基础练习 完美的代价   时间限制:1.0s   内存限制:512.0MB        锦囊1 使用贪心算法. 锦囊2 从左到右枚举每个字符,移动对应字符.个数为单的字符放中间.   问题描述 回文 ...

  7. Andriod——数据存储 SharedPrefrences

    xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...

  8. 正确配置Linux系统ulimit值的方法

    在Linux下面部署应用的时候,有时候会遇上Socket/File: Can’t open so many files的问题:这个值也会影响服务器的最大并发数,其实Linux是有文件句柄限制的,而且L ...

  9. bootstrap-slider插件使用方法

    bootstrap-slider例子地址:https://seiyria.com/bootstrap-slider/ bootstrap-slider github地址:https://github. ...

  10. js学习笔记12----json数据格式,语法,遍历

    1.json数据格式:var json={} 示例一: var user = {'name':'sese','age':'24','sex':'女'} console.log(user.age); / ...