六、继承与面向对象设计
six、Inheritance and Object-Oriented Design
条款 35:考虑virtual函数以外的其他选择
Rule 35:Differentiate between inheritance of interface and inheritance of implementation
这个条款主要通过一个继承体系来体现:
class GameCharacter {
public:
virtual int healthValue() const;
...
};
因为不同的人物会有不同的方式来计算,所以,需要将healthValue设置为 virtual 是非常正确的,但它并不是 纯虚函数,所以,我们有个计算健康值的缺省算法。
但是,这样做并不是最完美,它也有缺陷,所以,有没有其他方式来替代呢?
本条款就是介绍下列这些解决的方法。