<<Effective C++>>笔记5

Chapter 5: Implementation

Item 26:Postpone variable definition aslong as possible.

       Postponevariable definition as long as possible. It increases program clarity andimproves program efficiency.

       推迟一个变量的声明在C++中,比在C中重要得多。在C语言中,声明一个变量,开销仅仅是堆栈上的空间,而在C++中则意味着潜在运行开销(构造,析构)。

       推迟变量的声明不仅仅是指需要使用该变量之前进行声明,最好的方式是在为该变量准备好初始化参数的时候进行声明,也就是说在声明的时候就初始化。

 

Item 27: Minimize casting.

       1、Avoid casts whenever practical,especially dynamic_cast in performance-sensitive code. If a design requirescasting, try to develop a cast-free alternative.

       当需要使用cast的的时候,有可能是设计上出了问题,尤其是dynamic_cast.dynamic_cast是以来RTTI的,RTTI毕竟是有开销的。大部分情况之下没有必要使用dynamic_cast. C++推崇编译期绑定,dynamic_cast这种运行期才决定的运算,开销往往是很大而且没有必要的。

       需要使用dynamic_cast的情况是:对于一个对象,确定其是某派生类型对象,需要对该派生类型对象进行派生类的操作,但是确只有一个指向其的基类指针。有两种替代形式:1、存储不同继承层次对象类型的指针的容器。2、改为虚函数。基类中不用该函数时,该函数为空操作。

       这些改变都是设计层面的改变。

       2、When casting is necessary, try tohide it inside a function. Clients can then call the function instead ofputting casts in their own code.

       3、Prefer C++-style casts to old-stylecasts. They are easier to see, and they are more specific about what they do.

       C++提供了四种新的转换形式

const_cast<T>(expression)

用以改变变量的const属性。去除一个变量的const属性只能由该转换实现。dynamic_cast<T>(expression)

是一种RTII技术,可以将一个基类类型对象的引用或者指针转换为同一继层次中其他类型的引用或指针。

reinterpret_cast<T>(expression)

这个转换是产生与实现相关联结果的低层转换,很少用到。

static_cast<T>(expression)

一般的转换都可以由static_cast实现。也是最为常用的转换。

 

Item 28: Avoid returning “handles” toobject internals.

       Avoidreturning handles (references, pointers, or iterators) to object internals. Itincreases encapsulation, helps const member functions act const, and minimizesthe creation of dangling handles.

       这个是C++程序的问题,也是一个和语言无关的设计问题。当我们把一件事交个模块去做的时候,无论如何都不应该把模块的内部细节以任何方式暴露出来。从设计的角度看,唯一需要定义的是模块的输入输出。以性能为理由来暴露模块内部细节都是很牵强的。

 

Item 29: Strive for exception-safe code

       1、Exception-safe functions leak noresources and allow no data structures to become corrupted, even whenexceptions are thrown. Such functions offer the basic, strong, or nothrow guarantees.

       资源泄露和数据损坏是两种异常安全需要考虑的基本问题。

       异常安全的基本保障:当一个异常抛出时,没有资源泄露也没有数据损坏。所有的对象处于内部不变的状态,但是程序所处的特定状态是不可预知的。

       异常安全的强保障:当一个异常抛出时,程序的状态不改变,对这种强保障函数的调用时原子的,如果调用成功则程序改变状态,如果调用不成功则程序保持原来的状态。

       基本保障和强保障的区别就在于程序的状态是否会改变。

       有时,我们可以认可一种更低的异常安全等级,即没有资源泄露即可。在带自动垃圾回收的语言中,这是很容易满足的,但在C++中则不尽然。

       2、The strong guarantee can often beimplemented via copy-and-swap, but the strong guarantee is not practical forall functions.

       3、A function can usually offer aguarantee no stronger than the weakest guarantee of the functions it calls.

       异常安全的等级,是由该函数及该函数所调用的所有的函数的异常安全等级的最低等级来决定的。

 

Item 30: Understand the ins and outs ofinlining.

       1、Limit most inlining to small, frequentlycalled functions. This facilitates debugging and binary upgradability,minimizes potential code bloat, and maximizes the chances of greater programspeed.

       2、Don’t declare function templatesinline just because they appear in header files.

对于inline的使用,inline是使用在短小的函数上面的,并且最终时候是用inline来实现,

是由编译器来决定的,程序员定义inline函数是对编译器的一个请求而不是要求。

       函数调用的开销过程也要清楚。

 

Item 31: Minimize compilation dependenciesbetween files.

       该条目,用于大型程序,还没有看懂。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值