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++开发人员,大量时间会被花费在扩张你的类型系统上面.这意味着你不仅仅 ...
随机推荐
- CTreeCtrl 控件实现多选并取得选中项
刚开始以为实现起来很难,所以就在网上寻找实现的扩展控件,到最后才发现只要把CTreeCtrl 控件的Check Boxes 属性设为真就可以了,会在每个树形节点前添加一个CheckBox. 多选已经实 ...
- mysql 5.7.13 安装配置方法图文教程(win10) (转)
http://www.jb51.net/article/87152.htm ***************************** MySQL是一款关系型数据库管理系统,是由Oracle旗下公司M ...
- pssh安装和使用
前提,安装了python.pssh安装下载pssh-2.2.2.tar.gztar zxvf pssh-2.2.2.tar.gzcd pssh-2.2.2python setup.py buildpy ...
- SharePoint自动化系列——Solution auto-redeploy using Selenium(C#)
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 本来的想法是做一个可以自动卸载并且部署新solution到SharePoint farm的tool ...
- TensorFlow机器学习框架-学习笔记-001
# TensorFlow机器学习框架-学习笔记-001 ### 测试TensorFlow环境是否安装完成-----------------------------```import tensorflo ...
- 一款基于css3非常实用的鼠标悬停特效
今天给大家带来一款基于css3非常实用的鼠标悬停特效.这款特效,当鼠标经过时候一个半透明的遮罩层倒下来.效果很好,而且是纯css3实现的,代码很少,非常实用. 效果如下: 在线预览 源码下载 实现 ...
- 【Unity笔记】检测当前的运行平台
// 判断当前运行平台为Android平台时 if (Application.platform == RuntimePlatform.Android) { if (Input.GetKeyDown(K ...
- GNU make学习笔记
第五章:规则的命令 5.1 命令的回显 make在执行命令之前会把要执行的命令输出到标准输出设备,称之为"回显". 如果规则的命令以字符"@"开始,则make在 ...
- C语言 · 五次方数
算法提高 五次方数 时间限制:1.0s 内存限制:256.0MB 问题描述 对一个数十进制表示时的每一位数字乘五次方再求和,会得到一个数的五次方数 例如:1024的五次方数为1+0+ ...
- CodeIgniter(3.1.4)框架中添加执行时间统计代码
CodeIgniter(3.1.4)框架中添加,执行时间统计代码: system/core/CodeIgniter.php最后行处. /* * ---------------------------- ...