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 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的更多相关文章
- effective c++ 条款13 use object to manage resources.
请求的系统资源需要最终还回系统,为了避免遗忘返还这个动作,可以利用析构函数在object销毁时自动调用的特点来实现. 简单说就是用object来管理资源. 以内存资源为例 class Investme ...
- 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, ...
- 读书笔记 effective c++ Item 13 用对象来管理资源
1.不要手动释放从函数返回的堆资源 假设你正在处理一个模拟Investment的程序库,不同的Investmetn类型从Investment基类继承而来, class Investment { ... ...
- 读书笔记 effective c++ Item 15 在资源管理类中提供对原生(raw)资源的访问
1.为什么需要访问资源管理类中的原生资源 资源管理类是很奇妙的.它们是防止资源泄漏的堡垒,没有资源泄漏发生是设计良好的系统的一个基本特征.在一个完美的世界中,你需要依赖这样的类来同资源进行交互,绝不 ...
- 读书笔记 effective c++ Item 17 使用单独语句将new出来的对象放入智能指针
1. 可能会出现资源泄漏的一种用法 假设我们有一个获取进程优先权的函数,还有一个在动态分类的Widget对象上根据进程优先权进行一些操作的函数: int priority(); void proces ...
- 读书笔记 effective c++ Item 18 使接口容易被正确使用,不容易被误用
1. 什么样的接口才是好的接口 C++中充斥着接口:函数接口,类接口,模板接口.每个接口都是客户同你的代码进行交互的一种方法.假设你正在面对的是一些“讲道理”的人员,这些客户尝试把工作做好,他们希望能 ...
- 读书笔记 effective c++ Item 29 为异常安全的代码而努力
异常安全在某种意义上来说就像怀孕...但是稍微想一想.在没有求婚之前我们不能真正的讨论生殖问题. 假设我们有一个表示GUI菜单的类,这个GUI菜单有背景图片.这个类将被使用在多线程环境中,所以需要mu ...
- 读书笔记 effective c++ Item 31 把文件之间的编译依赖降到最低
1. 牵一发而动全身 现在开始进入你的C++程序,你对你的类实现做了一个很小的改动.注意,不是接口,只是实现:一个私有的stuff.然后你需要rebuild你的程序,计算着这个build应该几秒钟就足 ...
- 读书笔记 effective c++ Item 54 让你自己熟悉包括TR1在内的标准库
1. C++0x的历史渊源 C++标准——也就是定义语言的文档和程序库——在1998被批准.在2003年,一个小的“修复bug”版本被发布.然而标准委员会仍然在继续他们的工作,一个“2.0版本”的C+ ...
随机推荐
- jquery50个代码段
1. 如何创建嵌套的过滤器 ? 1 //允许你减少集合中的匹配元素的过滤器, //只剩下那些与给定的选择器匹配的部分.在这种情况下, //查询删除了任何没(:not)有(:has) //包含class ...
- uWSGI的各种部署方式
As a back-end with web-server, speak the uwsgi protocol <uwsgi id = "uwsgibk"> <s ...
- 线程安全的atomic wrapper classes例子
先参考一个例子 http://www.cnblogs.com/aigongsi/archive/2012/04/01/2429166.html#!comments 即使只是i++,实际上也是由多个原子 ...
- git过期处理
删除git下的文件:C:\Users\Java\AppData\Roaming\syntevo\......\7\settings.xml删除settings.xml文件即可
- anroid 广播
广播接收者(BroadcastReceiver)用于接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast().Context.sendOrderedBroa ...
- Arraylist静态初始化
new ArrayList<String>(Arrays.asList("ab","cd","ef"));
- r画饼图
原始图样: library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) p = ggplot ...
- View与Model绑定注意事项 (视图无数据显示)
Qt 中视图与模型绑定时,模型必须使用new来创建.否则刚开始初始化的时候,视图无数据显示,或者后期视图不能随着模型的改变而改变. 具体原因:我猜测是局部变量生命周期的问题.new 的变量在堆中,除非 ...
- Qt 事件过滤器
Qt创建了QEvent事件对象之后,会调用QObject的event()函数做事件的分发.有时候,你可能需要在调用event()函数之前做一些另外的操作,比如,对话框上某些组件可能并不需要响应回车按下 ...
- 关于Unity的入门游戏飞机大战的开发(下)
开发思路: 1: 修改测试模式,去掉开始按钮方便开发,加入敌机的资源2: 创建敌机 添加刚体,碰撞器组件,添加帧动画播放组件;3: 创建敌机出现的队形;4: 根据队形随机 生成我们的敌机,调整敌机的速 ...