天天看点

《Effective c++目录概要》——提升代码设计的55个忠告(32~55)

六、继承与面向对象设计(Inheritance andObject-Oriented Design):

32.     确定你的public继承塑模出is-a关系(make sure public inheritance models is “is-a.”

 (1)  “public继承”意味is-a。适用于base classes身上的每一件事情一定也适用于derived classes身上,因为每一个derived class对象也都是一个base class.

33.     避免遮掩继承而来的名称(avoiding hiding inherited names.)

(1) derived classes内的名称会遮掩base classes内的名称。在public继承下从来没有人希望如此。

(2) 为了让被遮掩的名称再见天日,可使用using声明式或转交函数(forwardingfunctions)。

34.     区分接口继承和实现继承(differentiate between inheritance of interface andinheritance of implementation.)

(1 )接口继承和实现继承不同。在public继承之下,derived class总是继承base class的接口。

(2)pure virtual函数只具体指定接口继承。

(3)简朴的(非纯)inpure virtual函数具体指定接口继承及缺省实现继承。

(4)Non_virtual函数具体指定接口继承以及强制性实现继承。

35.     考虑virtual函数以外的其他选择(consider alternatives to virtual functions.)

(1)virtual函数的替代方案包括NV1手法及Strategy设计模式的多种形式。NV1手法自身是一个特殊形式的Template Method设计模式。

(2)将机能从成员函数移到class外部函数,带来的一个缺点是,非成员函数无法访问class的non-public成员。

(3)Tr1::function对象的行为就像一般函数指针。这样的对象可接纳“与给定之目标签名式(target signature)兼容”的所有可调用物(callable entities)。

36.     绝不重新定义继承而来的non-virtual函数(neverredefine an inherited non-virtual function.)

(1)绝对不要重新定义继承而来的non-virtual函数。

37.     绝不重新定义继承而来的缺省参数值(never redefine a function’s inherited default parametervalue.)

(1)绝对不要重新定义一个继承而来的缺省参数值。因为缺省参数值都是静态绑定,而virtual函数——你唯一应该覆写的东西——却是动态绑定。

38.     通过复合塑模出has-a或“根据某物实现出”(model “has-a” or “is-implemented-in-terms-of” throughcomposition.

(1)复合(composition)的意义和public继承完全不同。

(2)在应用域(applicationdomain),复合意味has-a(有一个)。在实现域(implementationdomain),复合意味is-implemented-in-terms-of(根据某物实现出)。

39.     明智而审慎地使用private继承(use private inheritance judiciously.)

(1)private继承意味is-implemented-in-termsof(根据某物实现出)。它通常比复合(composition)的级别低。但是当derived class需要访问protected baseclass的成员,或需要重新定义继承而来的virtual函数时,这么设计是合理的。

(2)和复合不同,private继承可以造成empty base最优化。这对致力于“对象尺寸最小化”的程序库开发者而言,可能很重要。

40.     明智而审慎地使用多重继承(use multiple inheritance judiciously.)

(1)多重继承比单一继承复杂。它可能导致新的歧义性,以及对virtual继承的需要。

(2)Virtual继承会增加大小、速度、初始化(及赋值)复杂度等等成本。如果virtual baseclasses不带任何数据,将是最具实用价值的情况。

(3)多重继承的确有正当用途。其中一个情节涉及“public继承某个interface class”和“private继承某个协助实现的class”的两相组合。

七、模板与泛型编程(Templates and GenericProgramming):

41.     了解隐式接口和编译期多态(understand implicit interfaces and compile-time polymorphism.)

42.     了解typename的双重意义(understand the two meanings of typename.)

43.     处理模板化基类内的名称(know how to access names in templatized base classes.)

44.     将与参数无关的代码抽离templates(factor parameter-independent code out of templates.)

45.     运用成员函数模板接受所有兼容类型(use member function templates to accept “all compatibletypes.”

46.     需要类型转换时请为模板定义非成员函数(define non-member functions inside templates when typeconversions are desired.)

47.     请使用traits classes表现类型信息(usetraits classes for information about types.)

48.     认识template元编程(be aware of template metaprogramming.)

八、定制new和delete(Customizing new anddelete):

49.     了解new-handler的行为(understandthe behavior of the new-handler.)

50.     了解new和delete的合理替换时机(understand when it makes sense to replace new and delete.)

51.     编写new和delete时需固守常规(adhere to convention when writing new and delete.)

52.     New与delete成对出现(write placement delete if you write placement new.)

九、杂项讨论(Miscellany):

53.     不要轻忽编译器的警告(pat attention to complier warnings.)

54.     熟悉TR1在内的标准程序库(familiarize yourself with the standard library, includingTR1.)

55.     熟悉boost(familiarize yourself with boost.)

c++

继续阅读