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的更多相关文章

  1. 条款15:在资源管理类中提供对原始资源的访问(Provide access to raw resources in resource-managing classes)

    NOTE: 1.APIs往往要求访问原始资源(raw resources),所以每一个RAII class应该提供一个“取得其所管理之资源”的办法. 2.对原始资源的访问可能经由显示转换或隐式转换.一 ...

  2. [EffectiveC++]item15:Provide access to raw resources in resource-managing class

    在资源管理类中提供对原始资源的访问

  3. 读书笔记 effective c++ Item 15 在资源管理类中提供对原生(raw)资源的访问

    1.为什么需要访问资源管理类中的原生资源  资源管理类是很奇妙的.它们是防止资源泄漏的堡垒,没有资源泄漏发生是设计良好的系统的一个基本特征.在一个完美的世界中,你需要依赖这样的类来同资源进行交互,绝不 ...

  4. 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 ...

  5. 读书笔记 effective c++ Item 14 对资源管理类的拷贝行为要谨慎

    1. 自己实现一个资源管理类 Item 13中介绍了 “资源获取之时也是初始化之时(RAII)”的概念,这个概念被当作资源管理类的“脊柱“,也描述了auto_ptr和tr1::shared_ptr是如 ...

  6. 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 ...

  7. 读书笔记 effective c++ Item 19 像设计类型(type)一样设计

    1. 你需要重视类的设计 c++同其他面向对象编程语言一样,定义了一个新的类就相当于定义了一个新的类型(type),因此作为一个c++开发人员,大量时间会被花费在扩张你的类型系统上面.这意味着你不仅仅 ...

  8. 读书笔记 effective c++ Item 45 使用成员函数模板来接受“所有兼容类型”

    智能指针的行为像是指针,但是没有提供加的功能.例如,Item 13中解释了如何使用标准auto_ptr和tr1::shared_ptr指针在正确的时间自动删除堆上的资源.STL容器中的迭代器基本上都是 ...

  9. 读书笔记 effective c++ Item 19 像设计类型(type)一样设计类

    1. 你需要重视类的设计 c++同其他面向对象编程语言一样,定义了一个新的类就相当于定义了一个新的类型(type),因此作为一个c++开发人员,大量时间会被花费在扩张你的类型系统上面.这意味着你不仅仅 ...

随机推荐

  1. 关于一个页面的tab切换整体页面刷新而tab标签处是同一个文件怎么做焦点的问题

    解决方法,不能直接写点击效果就要在超链接中加一个参数,根据参数的值去给变焦点的效果,实现方法如下: <div class="vip_search">           ...

  2. 【WPF/WAF】主界面(ShellWindow)引入别的界面布局

    问题:主界面如果只用一个布局文件ShellWindow.xaml,会写得很大很臃肿.需要分为多个布局文件,然后由主界面引入.参考http://waf.codeplex.com/官方的BookLibra ...

  3. C语言 · 整除问题

    算法训练 整除问题   时间限制:1.0s   内存限制:512.0MB      问题描述 编写一个程序,输入三个正整数min.max和factor,然后对于min到max之间的每一个整数(包括mi ...

  4. git 远程仓库版本的回退以及git reset 几种常用方式记录

    由于 github push 了两个比较潦草的commit, 自己很不满意,又不想重新开vpn进行上传,所以找了一下相关的教程. 最后研究了一下,原理为先在本地还原到你想要的commit,然后强制pu ...

  5. ExtJs 常用小技巧备忘录

    1. ExtJs 给fieldLabel与fieldInput添加样式{给Input标签加入图标}http://www.w3school.com.cn/cssref/pr_background.asp ...

  6. OpenWRT中的按键和灯的GPIO控制实现

    基于BarrierBreaker版本,基于AR9331 AP121 Demo单板 来进行描述 1.灯 A.在mach-ap121.c中,定义了灯所对应的GPIO定义: #define AP121_GP ...

  7. BloomFilter——大规模数据处理利器[转]

    原文链接:原文 Bloom Filter是由Bloom在1970年提出的一种多哈希函数映射的快速查找算法.通常应用在一些需要快速判断某个元素是否属于集合,但是并不严格要求100%正确的场合. 一. 实 ...

  8. TCPdump抓包命令详解

    http://starsliao.blog.163.com/blog/static/89048201062333032563/ TCPdump抓包命令 tcpdump是一个用于截取网络分组,并输出分组 ...

  9. Zookeeper和分布式环境中的假死脑裂问题(转)

    Zookeeper和分布式环境中的假死脑裂问题 最近和同事聊天无意间发现他们的系统也存在脑裂的问题.想想当初在我们的系统中为了解决脑裂花了非常大的功夫,现在和大家一起讨论下脑裂,假死等等这些问题和解决 ...

  10. DataGridView使用技巧六:冻结列或行

    一.冻结列 DataGridViewColumn.Frozen属性为true时,该列左侧的所有列被固定,横向滚动时固定列不随滚动条滚动而左右移动.这对于重要列固定很有用. 示例:通过程序固定左侧第二列 ...