`

Effective c++学习笔记条款14:在资源管理类型中小心copying行为

 
阅读更多

Think carefully about copying behavior in resource-managing classes

在上一款中我们谈到,用资源管理类来管理动态分配的资源及如何运用auto_ptr和tr1::share_ptr管理
即使有了智能指针auto_ptr、引用计数型智能指针shared_ptr,有时难免还是需要实现自己的资源管理类型。假设Mutex类型通过lock和unlock两组函数进行互斥器的锁定和解锁,可能我们希望和auto_ptr一样的行为,在某个智能类型析构时主动调用unlock进行解锁。比如下面的代码:


当lockM2使用copy构造函数时,应该怎样做才是合理的?
一种方式是禁止复制,即将copy构造函数声明为private来阻止这种多份引用的行为。在第六条款中有提到的做法,派生于noncopyable类型。


另一种方法是使用资源引用计数法,直到最后一个使用者被销毁的时候,才进行真正意义上的销毁。在内部不再使用原类型的指针,而是改用tr1库的shared_ptr。shared_ptr不仅本身可以自行释放某个类型,同时它可以接受一个函数指针,自定义用户的删除方式。在线程锁的时候,我们只是希望解锁并不是删除Mutex。


请记住:
◆ 复制RAII对象必须一并复制它所管理的资源,所以资源的copying行为决定RAII对象的copying行为.
◆ 普遍而常见的RAII class copying行为是:抑制copying、施行引用计数法.不过其它行为也都可能被实现.

分享到:
评论

相关推荐

    Effective C++(第三版)

    条款14:在资源管理类中小心coping行为 think carefully about copying behavior in resource-managing classes. 条款15:在资源管理类中提供对原始资源的访问 provide access to raw resources in resource-...

    Effective Modern C++ 原版pdf by Meyers

    and C++14, but instead to their effective application. The information in the book is broken into guidelines called Items. Want to understand the various forms of type deduction? Or know when (and ...

    c++ Effective STL(中文+英文)

    条款7: 当使用new得指针的容器时,切记在容器销毁前delete那些指针 条款8: 千万不要把auto_ptr放入容器中 条款9: 小心选择删除选项 条款10: 当心allocator的协定和约束 条款11: 了解自定义allocator的正统使用法...

    高效现代C++(Effective Modern C++)(高清英文原版)

    If you’re an experienced C++ programmer and are anything like me, you initially approached C++11 thinking, “Yes, yes, I get it. It’s C++, only more so.” But as you learned more, you were surprised...

    Mac安装盘制作工具11.0.zip

    windows下超级简单制作10.8/10.9/10.10/10.11安装系统的优盘工具,简单到你无法想象。

    C++标准库(第二版)英文版.pdf

    The C++ Standard Library A Tutorial and Reference (2nd Edition)+cppstdlib-code.zip C++标准库(第二版)英文版.pdf 非扫描版+源代码 Prefaceto the SecondEdition xxiii Acknowledgments for the Second...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Another useful rule of thumb: it's typically not cost effective to inline functions with loops or switch statements (unless, in the common case, the loop or switch statement is never executed)....

    Beaglebone Black裸机MLO文件

    Copying application image from MMC/SD card to RAM Jumping to StarterWare Application... 但并没有实际现象。 原因是官方的Beaglebone MLO是DDR2的,而Beaglebone Black是DDR3的。 压缩包内含MLO和app两个文件...

    Twitter API Client(gridtwit) oauth demo C++源代码

    这里拿出来共享,是希望大家在做同类产品时,在 OAUTH 鉴权过程中,可以参考这个,并少花点时间。 文件清单: gridtwit_client_oauth_demo.tar.gz file list: | Demo_Description.doc | gridtwit_run_output_info....

    C++程序设计语言(特别版) 英文原版

    guage in itself.This book demonstrates key techniques that make C++effective and teaches the fundamental concepts necessary for mastery.Except where illustrating technicalities,examples are taken from...

    copying.txt

    devcpp所需的必须文件。devcpp所需的必须文件。

    QtCoreUtilities:通用 C++ 库,依赖于 QtCore 库

    vedgTools/QtCoreUtilities 在GNU GPLv3+许可下获得许可,其副本可以在COPYING文件中找到。 vedgTools/QtCoreUtilities 是免费软件:您可以根据自由软件基金会发布的 GNU 通用公共许可证(许可证的第 3 版或(由您...

    Visual C++ 编程资源大全(英文源码 图形)

    1,01.zip Displaying a 256 color bitmap 在程序中显示256色的位图(6KB)<END><br>2,02.zip Creating a bitmap object from a BMP file 从位图文件中创建位图对象(6KB)<END><br>3,03.zip An auto-...

    SecureCode Protection for Delphi & C++ Builder

    SecureCode is a new software protection designed to provide a high level of security from illegal copying, and confidence that people can only run your software on your terms. The security is internal...

    Copying_a_Maximo_Schema_on_Oracle

    Copying_a_Maximo_Schema_on_Oracle

    android的GC内存泄露问题

    其实如果我们一个程序中,已经不再使用某个对象,但是因为仍然有引用指向它,垃圾回收器就无法回收它,当然该对象占用的内存就无法被使用,这就造成了内存泄露。如果我们的java运行很久,而这种内存泄露不断的发生,...

    cocos2d-x c++的iconv.rar

    see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /* When installed, this file is called "iconv.h". */ ...

    A JCL STEOP ABOUT COPYING PDS DATASET.

    A JCL STEOP ABOUT COPYING PDS DATASET.

    Oracle Solaris 11.3 Copying and Creating Package Repositories in

    Oracle Solaris 11.3 Copying and Creating Package Repositories in Oracle Solaris 11.3-104

Global site tag (gtag.js) - Google Analytics