Effective C++ 读书笔记3

本文探讨了面向对象编程中继承与组合的概念及其应用。详细解释了公共继承、私有继承和多重继承的特点与注意事项,包括如何正确重定义函数、避免隐藏基类名称等问题。

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

Item 29
Public inheritance means “is-a”. Everything that applies to base classed must also applu to derived classes, because derived class object is a base class object.

Item 30
1. Names in derived classes hide names in base classes. Under public inheritance, this is never desirable.
2. To make hidden names visible again, employ using declarations or forwarding functions.

Item 31
1. Inheritance of interface is different from inheritance of implementation. Under public inheritance, derived classes inherit base class interfaces.
2. Pure virtual functions specify inheritance of interface only.
3. Simple (impure) virtual functions specify inheritance of interface plus inheritance of a default implementation.
4. Non-virtual functions specify inheritance of interface plus inheritance of a mandatory implementation.

Item 32
1. Alternatives to virtual functions include the NVI idiom and various forms of the Strategy design pattern. The NVI idiom is itself an example of the Template Method design pattern.
2. A disadvantage of moving functionality from a member function to a function outside the class is that the non-member function lacks access to the class’s non-public members.
3. tr1::function objects act like generalized function pointers. Such objects support all callable entities compatible with a given target signature.

Item 33
Never redefine an inherited non-virtual function.

Item 34
Never redefine an inherited default parameter value, because default parameter values are statically bound, while virtual functions–the only functions you should be overriding–are dynamically bound.

Item 35
1. Composition has meanings completely different from that of public inheritance.
2. In the application domain, composition means has-a. In the implementation domain, it means is-implemented-in-terms-of.

Item 36
1. Private inheritance means is-implemented-in-terms of. It’s usually inferior to composition, but it makes sense when a derived class needs access to protected base class members or needs to redefine inherited virtual functions.
2. Unlike composition, private inheritance can enable the empty base optimization. This can be important for library developers who strive to minimize object sizes.

Item 37
1. Multiple inheritance is more complex than single inheritance. It can lead to new ambiguity issues and to the need for virtual inheritance.
2. Virtual inheritance imposes costs in size, speed, and complexity of initialization and assignment. It’s most practical when virtual base classes have no data.
3. Multiple inheritance does have legitimate uses. One scenario involves combining public inheritance from an Interface class with private inheritance from a class that helps with implementation.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值