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, we encounter a situation where an API accept raw resource instead of RAII object. For example:
// You have a shared pointer
std::tr1::shared_ptr<Foo> foo(createFoo()); // But the API only accept Foo
void acceptFoo(Foo *foo);
In this situation, you need RAII object return raw resources. And there are commonly two ways to do that
1. Explicitly provide method to return raw resource of RAII object. That's how shared_ptr handles this situation. shared_ptr have get() method to return raw resource it contains. In addition, it overload -> and * method. This way is safe and clear, but still some make might think it not natural, and we have a second way to accomplish it.
2. Implicitly convert RAII object to raw resource.
How to do that? Let's see a example
class Font {
public:
...
operator FontHandle() const {
return f;
}
private:
FontHandle f;
};
We overload operator () to implicitly convert Font to FontHandle and eliminate the use of get(). But this way have a downside.
Font f1(getFont());
// oops, intent to copy a Font object, but wrongly write FontHandler
// yet implicitly convertion hold the candle to the devil
FontHandler f2 = f1;
If f1 is destroyed and FontHandler is released then f2 become dangle pointer.
Effective C++ Item 15 Provide access to raw resources in resource-managing classes的更多相关文章
- 条款15:在资源管理类中提供对原始资源的访问(Provide access to raw resources in resource-managing classes)
NOTE: 1.APIs往往要求访问原始资源(raw resources),所以每一个RAII class应该提供一个“取得其所管理之资源”的办法. 2.对原始资源的访问可能经由显示转换或隐式转换.一 ...
- [EffectiveC++]item15:Provide access to raw resources in resource-managing class
在资源管理类中提供对原始资源的访问
- 读书笔记 effective c++ Item 15 在资源管理类中提供对原生(raw)资源的访问
1.为什么需要访问资源管理类中的原生资源 资源管理类是很奇妙的.它们是防止资源泄漏的堡垒,没有资源泄漏发生是设计良好的系统的一个基本特征.在一个完美的世界中,你需要依赖这样的类来同资源进行交互,绝不 ...
- 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 ...
- 读书笔记 effective c++ Item 14 对资源管理类的拷贝行为要谨慎
1. 自己实现一个资源管理类 Item 13中介绍了 “资源获取之时也是初始化之时(RAII)”的概念,这个概念被当作资源管理类的“脊柱“,也描述了auto_ptr和tr1::shared_ptr是如 ...
- Effective C++ Item 14 Think carefully about copying behavior in resource-managing classe
In C++, the only code that guaranteed to be executed after an exception is thrown are the destructor ...
- 读书笔记 effective c++ Item 19 像设计类型(type)一样设计
1. 你需要重视类的设计 c++同其他面向对象编程语言一样,定义了一个新的类就相当于定义了一个新的类型(type),因此作为一个c++开发人员,大量时间会被花费在扩张你的类型系统上面.这意味着你不仅仅 ...
- 读书笔记 effective c++ Item 45 使用成员函数模板来接受“所有兼容类型”
智能指针的行为像是指针,但是没有提供加的功能.例如,Item 13中解释了如何使用标准auto_ptr和tr1::shared_ptr指针在正确的时间自动删除堆上的资源.STL容器中的迭代器基本上都是 ...
- 读书笔记 effective c++ Item 19 像设计类型(type)一样设计类
1. 你需要重视类的设计 c++同其他面向对象编程语言一样,定义了一个新的类就相当于定义了一个新的类型(type),因此作为一个c++开发人员,大量时间会被花费在扩张你的类型系统上面.这意味着你不仅仅 ...
随机推荐
- [gulp入门]gulp-connect浏览器自动刷新
LiveReload可以理解为即时刷新,在前端开发中,开发者在编写或调试html/js/css代码后需要从编辑器切换到浏览器,再刷新浏览器才能看到页面变化,这种频繁的操作在一定程度上影响了工作效率,而 ...
- ubuntu 文档查看器/gedit查看txt中文乱码问题
文档查看器界面是中文的,但查看pdf文档只显示英文,中文都空了出来. 用命令: sudo apt-get install poppler-data 解决该问题. gedit查看txt中文乱码问题 打开 ...
- pyqt重写键盘事件+获取信号发送对象
# _*_ coding:utf-8 _*_ import sys from PyQt4 import QtGui,QtCore class Example(QtGui.QMainWindow): d ...
- 服务器响应慢的分析与解决(Linux服务器)
一.分析思路 1.排除本机自身原因 2.服务器性能分析 3.项目本身分析(不详细说) 4.虚拟机分析 5.数据库分析 二.详细分析方法 1.排除本机自身原因 可以使用站长工具测试网站速度. 2.服务器 ...
- C#基础-获得当前程序的 空间名.类名.方法名
string typeName = this.GetType().ToString();//空间名.类名 string typeName = this.GetType().Name;//类名 new ...
- 【C#/WPF】调节图像的HSL(色相Hue、饱和度Saturation、明亮度Lightness)
先说概念: HSL是一种描述颜色的方式,其他颜色描述方式还有大家熟悉的RGB值.HSL三个字母分别表示图像的Hue色相.Saturation饱和度.Lightness明亮度. 需求: 制作一个面板,包 ...
- size_t ssize_t socklen_t
size_t 解释一:为了增强程序的可移植性,便有了size_t,它是为了方便系统之间的移植而定义的,不同的系统上,定义size_t可能不一样. 在32位系统上 定义为 unsigned int 也就 ...
- 一站式学习Wireshark(一):Wireshark基本用法
按照国际惯例,从最基本的说起. 抓取报文: 下载和安装好Wireshark之后,启动Wireshark并且在接口列表中选择接口名,然后开始在此接口上抓包.例如,如果想要在无线网络上抓取流量,点击无线接 ...
- Idea创建sbt项目
这篇文章开始演示如何使用sbt新建项目. 1. 新建,选择Scala,SBT 填写项目名称,Scala版本号等信息. 单击完成后,后台会一直处于下载依赖文件的状态,等吧.. 最终后台运行完成后的完整目 ...
- github搭建个人博客----------绑定域名访问
首先你得有一个Github账号,没有的话去github.com注册一个账号,然后到达仓库信息填写界面: 创建仓库,如下图:(仓库名要以自己的github名作为前缀,后面的夹 .github.io) 后 ...