Effective C++ 读书笔记2

本文深入探讨了通过避免返回对象内部引用、提供不同级别的异常保证、限制内联范围以及合理管理资源来提高代码质量的方法。详细解释了基本、强和无抛出保证的概念,并提供了实施这些原则的策略,如使用复制和交换实现强保证,以及如何通过声明依赖而非定义依赖来最小化编译依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Item 25
Avoid returning handles(references, pointers, or iterators) to object internals. It increases encapsulation, helps const member functions act const, and minimizes the creation of dangling handles.

Item 26
1. Functions offering the basic guarantee promise that if an exception is thrown, everything in the program remains in a valid state.
2. Functions offering the strong guarantee promise that if an exception is thrown, the state of the program is unchanged.
3. Functions offering the nothrow guarantee promise never to throw exceptions, because they always do what they promise to do.

  • Exception-safe functions leak no resources and allow no data structures to become corrupted, even when exceptions are thrown. Such functions offer the basi, strong, or nothrow guarantees.
  • The strong guarantee can often be implemented via copy-and-swap,but the strong guarantee is not practical for all functions.
  • A function can usually offer a guarantee no stronger than the weakest guarantee of the functions it calls.

Item 27
1. Limit most inlining to small, frequently called functions. This facilitates debugging and binary upgradability, minimizes potential code bloat, and maximizes the chances of greater program speed.
2. Don’t declare function templates inline just because they appear in header files.

Item 28
1. The general idea behind minimizing compilation dependencies is to depend on declarations instead of definitions. Two approached based on this idea are Handle classed and Interface classed.
2. Library header files should exist in full and declaration-only forms. This applies regardless of whether templates are involved.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值