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. UBuntu14.04下安装和卸载Qt5.3.1

    安装: 1. Qt5.3.1下载地址为:http://qt-project.org/,选择”Qt 5.3.1 for Linux 32-bit”版本,文件名是”qt-opensource-linux- ...

  2. python traceback捕获并打印异常

    异常处理是日常操作了,但是有时候不能只能打印我们处理的结果,还需要将我们的异常打印出来,这样更直观的显示错误 下面来介绍traceback模块来进行处理 try: 1/0 except Excepti ...

  3. FreeRTOS 独立看门狗监测任务执行状态

    以下转载自安富莱电子: http://forum.armfly.com/forum.php 通过前面的几个章节,我们基本已经完成了 FreeRTOS 所有功能的讲解,本章节为大家介绍一种使用独立看门狗 ...

  4. svn 使用笔记(一)

    检出: svn checkout http://***.***.***.*** path --username *** --password ***   更新: svn update path --f ...

  5. phalcon的url大小写的问题

    一开始我以为url的大小写是不区分的,实际上调试时是可以发现获取到的url是大小写是和请求时一致, 所谓的没区分,只是服务器或者相应的代码做的处理. 在phalcon里如果路由是api/test,则会 ...

  6. scala实现彩票算法

    scala实现彩票算法 (1)具体实现代码如下: package hw1 import scala.util.control._ /** * @author BIGDATA */ object Cp ...

  7. mongo 内存限制wiredTigerCacheSizeGB = 10

    [root@iZ2zed126f44v90yv59ht3Z rabbitmq]# cat /usr/local/mongodb/mongodb.confport = 27017dbpath = /us ...

  8. 关于c# 发射的调用并进行缓存

    private static object CreateObject(string AssemblyPath, string classNamespace) { object objObject = ...

  9. java list分组 list里面分装的都是对象 按照对象的属性来分组

    http://www.iteye.com/problems/86110 —————————————————————————————————————————————————————————— List& ...

  10. [mysql] 随机查询 效率比较

    select primary_count as primaryCount, primary_score as primaryScore, junior_count as juniorCount, ju ...