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. 双系统中ubuntu的安装方法

    双系统中ubuntu的安装方法 注意:给电脑安装双系统时,一定要先装Windows系统,再安装Linux系统! 原因是电脑开机后,要先执行一段bootloader引导程序:再由引导程序启动操作系统.W ...

  2. 【Maven】maven打包生成可执行jar文件

    http://blog.csdn.net/u013177446/article/details/53944424 ******************************************* ...

  3. 基于jquery右侧悬浮加入购物车代码

    分享一款基于jquery右侧悬浮加入购物车代码.这是一款基于jQuery实现的仿天猫右侧悬浮加入购物车菜单代码. 在线预览   源码下载 实现的代码: <!--左侧产品parabola.js控制 ...

  4. json关键总结

    先引用 Newtonsoft.Json.Net20.dll //序列化对象的方法也非常的简单: string json = JsonConvert.SerializeObject(product); ...

  5. Java中HashMap实现原理

    类声明: 概述: 线程不安全: <Key, Value>两者都可以为null: 不保证映射的顺序,特别是它不保证该顺序恒久不变: HashMap使用Iterator: HashMap中ha ...

  6. 【WPF】XAML引入资源和在C#代码中动态添加样式

    转载自: http://blog.csdn.net/honantic/article/details/48781543 XAML引入资源参考这里: http://blog.csdn.net/qq_18 ...

  7. uboot中bootargs实现

    setup.h通过宏定义实现了bootargs传递参数到内核,值得以后编程学习. include/asm-arm/setup.h 14  * NOTE: 15  *  This file contai ...

  8. Web API(五):Web API跨域问题

    一.什么是跨域问题 跨域:指的是浏览器不能执行其他网站的脚本.是由浏览器的同源策略造成的,是浏览器施加的安全限制.(服务端可以正常接收浏览器发生的请求,也可以正常返回,但是由于浏览器的安全策略,浏览器 ...

  9. C# 关于JArray和JObject封装JSON对象

    直入主题,不废话... 1.JObject:基本的json对象 /// <summary> /// Gets the j object. /// </summary> /// ...

  10. 如何查看机器是否为vmware虚拟机

    vmware虚拟机的网卡MAC地址一般都是005056开头的,可用ifconfig看一下,也可用dmesg |grep vm查看 艺搜参考 http://bbs.chinaunix.net/threa ...