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. [转]编写 android.mk 中 LOCAL_C_INCLUDES 的技巧

    看原文请移步:编写 android.mk 中 LOCAL_C_INCLUDES 的技巧 在编写android.mk的过程中,免不了要修改LOCAL_C_INCLUDES来设置头文件的include目录 ...

  2. Lo4j(二)级别和优化

    在log里面有日志级别: DEBUG Level: 指出细粒度信息事件对调试应用程序是非常有帮助的,就是输出debug的信息.INFO level: 表明消息在粗粒度级别上突出强调应用程序的运行过程, ...

  3. uWSGI的stats注释,送给需要的人,欢迎指正

    吐槽先,对于uWSGI状态信息没有文档说明这样一个现实,我只想说一句:F*CK YOU!!! 花了2天时间,累得眼珠子疼,针对这鬼畜的stats,借助Total Commander和VS大概撸了一边u ...

  4. exit和wait一起可以彻底清除子进程的资源

    #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<stdlib.h> ...

  5. git关联远程仓库命令<原>

    一.存在远程仓库了,本地想克隆其代码: $ git clone git@git.oschina.net:winkey4986/Weather_demo.git 二.本地有代码了,想在建个远程仓库保存代 ...

  6. 修改 login的串口重定向

     1 在console-telnet 使用vi工具编辑 /etc/inittab 文件  vi /etc/inittab (回车)2 按 i 进入编辑模式:3 将文件中的ttyS0  改为 ttyS3 ...

  7. iOS开发小技巧--定义宏和pch文件的使用

    一.创建pch文件,默认跟项目同名 二.告诉系统,编译的时候要编译pch文件的步骤 三.把经常用到的宏  或者  分类 包含到这里

  8. 利用jQuery进行三行两列等高布局

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. [转自setting]神奇的jQuery

    前言 之前的项目也一直都是在用jQuery,遇到问题就翻翻API,也从来没有进行过比较系统的总结和整理.最近由于要做个培训,因为比较系统的归纳了一下javascript的相关知识,顺手做个笔记.说到j ...

  10. android 编译 app

    有些编写的app需要放到android的源码中进行编译.放置的路径packages/apps/ 编译方法,参考 http://blog.csdn.net/luoshengyang/article/de ...