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. Spring Boot干货系列:(四)Thymeleaf篇

    Spring Boot干货系列:(四)Thymeleaf篇 原创 2017-04-05 嘟嘟MD 嘟爷java超神学堂 前言 Web开发是我们平时开发中至关重要的,这里就来介绍一下Spring Boo ...

  2. android中activity向service中传值

    和activity中互相传值类似 在activity中 Intent regIntent = new Intent(this, ChatService.class);  regIntent.putEx ...

  3. Consumer

    Description FJ is going to do some shopping, and before that, he needs some boxes to carry the diffe ...

  4. linux 基本配置tab键和显示行号 和中文输入法

    一.仅设置当前用户的Tab键宽度 输入命令:vim ~/.vimrc 然后:set tabstop=4   //我这里将Tab键的宽度设置为4 保存:ctrl+z+z(或:wq!) OK! 二.设置所 ...

  5. spring通过配置xml文件集成quartz定时器

    概述 Spring为创建Quartzde Scheduler.Trigger和JobDetail提供了方便的FactoryBean类,以便能够在Spring容器中享受注入的好处. 此外,Spring还 ...

  6. am335x PDK3.0 设置为单网口配置记录

    原来的配置是双网口的,现在要配置为单网口. 一直以为这个配置是在 make menuconfig 里面, 没想到是在设备树里面. 修改设备树 // vim arch/arm/boot/dts/am33 ...

  7. ExtJs 通过分析源代码解决动态加载Controller的问题

    通过分析源代码解决动态加载Controller的问题 最近在研究ExtJs(4.2.0)的MVC开发模式,具体Extjs的MVC如何使用这里不解释,具体参见ExtJs的官方文档.这里要解决的问题是如何 ...

  8. hive输出json字符串

    目前没发现有什么方便的函数可以直接使用,只能使用concat来手工拼接. 注意将null的字段值转为空,使用nvl函数 如果将hql语句写在script.q文件里面如下: select concat( ...

  9. [Django学习]Ajax访问静态页面

    Web开发中常用的一种开发方式是:通过Ajax进行系统的交互,采用Ajax进行交互的时候,更多的时候传输的是JSON格式的数据. 所以开发中我们需要将数据格式转化成JSON,请参见:https://w ...

  10. CodeIgniter(3.1.4)框架中-使用多个公共控制器

    项目目录结构: 在core/MY_Controller.php文件下: <?php /** * Class MY_Controller * 自定义控制器 */ class MY_Controll ...