<Item 15> Provide access to raw resources in resource-managing classes

1、You need a way to convert an object of the RAII class (in this case, tr1::shared_ptr) into the raw resource it contains (e.g., the underlying Investment*). There are two general ways to do it: explicit conversion and implicit conversion.

tr1::shared_ptr and auto_ptr both offer a get member function to perform an explicit conversion, i.e., to return (a copy of) the raw pointer inside the smart pointer object:

int days = daysHeld(pInv.get());            // fine, passes the raw pointer
// in pInv to daysHeld

Like virtually all smart pointer classes, TR1::shared_ptr and auto_ptr also overload the pointer dereferencing operators (operator-> and operator*), and this allows implicit conversion to the underlying raw pointers:

class Investment {                         // root class for a hierarchy
public: // of investment types
bool isTaxFree() const;
...
};
Investment* createInvestment(); // factory function
std::tr1::shared_ptr<Investment> // have tr1::shared_ptr
pi1(createInvestment()); // manage a resource bool taxable1 = !(pi1->isTaxFree()); // access resource
// via operator->
...
std::auto_ptr<Investment> pi2(createInvestment()); // have auto_ptr
// manage a
// resource bool taxable2 = !((*pi2).isTaxFree()); // access resource
// via operator*
...

2、The Font class could offer an explicit conversion function such as get:

class Font {
public:
...
FontHandle get() const { return f; } // explicit conversion function
...
};

Unfortunately, this would require that clients call get every time they want to communicate with the API:

void changeFontSize(FontHandle f, int newSize);     // from the C API
Font f(getFont());
int newFontSize;
... changeFontSize(f.get(), newFontSize); // explicitly convert
// Font to FontHandle

Some programmers might find the need to explicitly request such conversions off-putting enough to avoid using the class. That, in turn, would increase the chances of leaking fonts, the very thing the Font class is designed to prevent.

The alternative is to have Font offer an implicit conversion function to its FontHandle:

class Font {
public:
...
operator FontHandle() const { return f; } // implicit conversion function
...
};

That makes calling into the C API easy and natural:  但是隐式转换可能会偷偷造成不想要的转换

Font f(getFont());
int newFontSize;
... changeFontSize(f, newFontSize); // implicitly convert Font
// to FontHandle

3、Often, an explicit conversion function like get is the preferable path, because it minimizes the chances of unintended type conversions. Sometime, however, the naturalness of use arising from implicit type conversions will tip the scales in that direction.但是一切以Item18为原则。Furthermore, some RAII classes combine true encapsulation of implementation with very loose encapsulation of the underlying resource. For example, tr1::shared_ptr encapsulates all its reference-counting machinery, but it still offers easy access to the raw pointer it contains. Like most well-designed classes, it hides what clients don't need to see, but it makes available those things that clients honestly need to access.

4、Things to Remember

  • APIs often require access to raw resources, so each RAII class should offer a way to get at the resource it manages.

  • Access may be via explicit conversion or implicit conversion. In general, explicit conversion is safer, but implicit conversion is more convenient for clients.

<Item 16>Use the same form in corresponding uses of new and delete.

5、When you employ a new expression (i.e., dynamic creation of an object via a use of new), two things happen. First, memory is allocated (via a function named operator new—see Items 49 and 51). Second, one or more constructors are called for that memory. When you employ a delete expression (i.e., use delete), two other things happen: one or more destructors are called for the memory, then the memory is deallocated (via a function named operator delete—see Item 51).

6、 In particular, the memory for an array usually includes the size of the array, thus making it easy for delete to know how many destructors to call. The memory for a single object lacks this information. This is just an example, of course. Compilers aren't required to implement things this way, though many do.

7、This is a particularly important rule to bear in mind when you are writing a class containing a pointer to dynamically allocated memory and also offering multiple constructors, because then you must be careful to use the same form of new in all the constructors to initialize the pointer member. If you don't, how will you know what form of delete to use in your destructor?

8、This rule is also noteworthy for the typedef-inclined, because it means that a typedef's author must document which form of delete should be employed when new is used to conjure up objects of the typedef type. For example, consider this typedef:

typedef std::string AddressLines[];   // a person's address has 4 lines,
// each of which is a string

Because AddressLines is an array, this use of new,

std::string *pal = new AddressLines;   // note that "new AddressLines"
// returns a string*, just like
// "new string[4]" would

must be matched with the array form of delete:

delete pal;                           // undefined!
delete [] pal; // fine

delete 和delete []用错了的话,The result is undefined。使用string和vector来避免动态分配数组。

9、Things to Remember

  • If you use [] in a new expression, you must use [] in the corresponding delete expression. If you don't use [] in a new expression, you mustn't use [] in the corresponding delete expression.

<Item 17>Store newed objects in smart pointers in standalone statements.

10、C++的编译器相比java和C#给予更多语句执行顺序的自由度,exception可能打断正常的执行流程,导致资源泄露

int priority();
void processWidget(std::tr1::shared_ptr<Widget> pw, int priority);
processWidget(new Widget, priority()); //不会编译,没有定义好的隐式转换
processWidget(std::tr1::shared_ptr<Widget>(new Widget), priority());
  1. Execute "new Widget".

  2. Call priority.

  3. Call the tr1::shared_ptr constructor.

可以修正如下

std::tr1::shared_ptr<Widget> pw(new Widget);  // store newed object
// in a smart pointer in a
// standalone statement processWidget(pw, priority()); // this call won't leak

11、Things to Remember

  • Store newed objects in smart pointers in standalone statements. Failure to do this can lead to subtle resource leaks when exceptions are thrown.

最新文章

  1. 每天一个linux命令(51):lsof命令
  2. CSS代码重构与优化之路
  3. 解决英文或数字在HTMl网页中不自动换行。
  4. ndk-gdb 对java/native code联合调试(升级版)
  5. BZOJ 2727 双十字(树状数组)
  6. C++ 泛型编程/模板 泛函编程/Lambda/λ演算
  7. sql 删除表数据truncate delete drop的区别
  8. 获取EnterpriseLibrary企业库配置文件中ConnectionStrings(原创)
  9. SQL点滴12—SQL Server备份还原数据库中的小把戏
  10. ios 中NSArray
  11. react系列从零开始-react介绍
  12. 入侵检测工具之RKHunter &amp; AIDE
  13. mysql 手册关于修改列字符编码的一个bug
  14. I/O和管道
  15. ES 16 - 对Elasticsearch中的索引数据进行增删改查 (CRUD)
  16. 核心类生成-Mybatis Generator的使用
  17. C++ template一些体悟(2)
  18. Service Discovery And Health Checks In ASP.NET Core With Consul
  19. 以太坊(Ethereum)智能合约NodeJS/Web3 使用
  20. int 和 Integer 的区别

热门文章

  1. Linux计划任务crontab设置详解
  2. WinSCP与Putty远程连接linux
  3. java 第七章 面向对象高级特性
  4. python中将datetime对象转化为时间戳
  5. spring源码-增强容器xml解析-3.1
  6. SQL计算出百分比
  7. JS学习 用 arguments 对象模拟函数重载
  8. jQuery wordexport导出 word
  9. 05-JVM对象探秘
  10. Windows运行机理——窗口和句柄