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++开发人员,大量时间会被花费在扩张你的类型系统上面.这意味着你不仅仅 ...
随机推荐
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- VBA学习笔记(5)--事件,记录每次操作改动
说明(2017.3.24): 1. 记录sheet1里面的每次改动,和改动时间! 2. 不能记录大范围的删除.改动,会提示“类型不匹配” Private Sub Worksheet_Change(By ...
- 手风琴式焦点图jQuery特效
手风琴式焦点图jQuery特效是一款鼠标点击人物图像滑动切换案例说明信息代码.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div class="ag-cont ...
- python -- 字符串和编码
字符串和编码 数字--文本 ascii(bg2312,shift_jis,eur_kr)--unicode--utf-8 ord(""),chr() 1 Python提供了ord ...
- SQL server 2005如何设置一个或几个字段唯一约束?
--建立是在三个字段上的唯一约束 alter table Tab add constraint uq_id unique(shipType, shipsession, Territory)
- 获取本地IP和mac等信息
1获取mac protected string getHostMacName() { string mac = ""; ManagementClass mc; mc = new M ...
- PHP 之前后端交互
今天想实现一个很简单的功能,前端上面一个按键去控制 板子里面的一个LED灯,就这么一个简单的问题,困扰了一天. 以前搞底层的时候,没有服务端和客户端的深刻概念,网页不同,分在客户端运行的 html 代 ...
- JavaScript 深入理解作用域链
第一步. 定义后:每个已定义函数,都有一个内在属性[scope],其对应一个对象的列表,列表中的对象仅能内部访问. 例如:建立一个全局函数A,那么A的[Scope]内部属性中只包含一个全局对象(Glo ...
- 推断iframe里的页面载入完毕
//推断iframe是否载入完毕,RMid为iframe的ID document.getElementById("RMid").onload = function () { ale ...
- javaScript之function定义
背景知识 函数定义 在javaScript中,function的定义有3种: 1.匿名定义 function(){} 2.非匿名定义 fun ...