细读《Effective C++》之九

Chapter 6. Inheritance and Object-Oriented Design

 

条款36:Never redefine an inherited non-virtual function

class  B {
public
:
  
void
 mf();
  ...
};

class D: public
 B {
public
:
  
void
 mf();
 ... 
 };

D x;                              
// x is an object of type D

*pB = &x;                       // get pointer to x
pB->mf();                         // calls B::mf
*pD = &x;                       // get pointer to x
pD->mf();                         // calls D::mf

non-virtual functions是statically bound,此处mf的调用取决于其声明类型而非所指对象;virtual functions是dynamically bound,如果mf是virtual function,其调用将取决于其所指对象,即D。

这儿,出于以下两点原因,Never redefine an inherited non-virtual function:

1) public inheritance应保证is-a关系。

2) non-virtual function的不变性(invariant)应凌驾于特异性(specialization)。

Things to Remember

1) Never redefine an inherited non-virtual function.

条款37:Never redefine a function's inherited default parameter value

redefine a function,那么这个function一定是virtual function了,virtual functions are dynamically bound, but default parameter values are statically bound。

// a class for geometric shapes
class  Shape {
public
:
  
enum
 ShapeColor { Red, Green, Blue };
  
// all shapes must offer a function to draw themselves

  virtual void draw(ShapeColor color = Red) const = 0 ;
  ...
};

class Rectangle: public
 Shape {
public
:
  
// notice the different default parameter value — bad!

  virtual void draw(ShapeColor color = Green) const ;
  ...
};

Shape 
*ps = new Rectangle;       // static type = Shape*, dynamic type = Rectangle*

ps->draw(Shape::Red);            // calls Rectangle::draw(Shape::Red)
ps->draw();                      // calls Rectangle::draw(Shape::Red)!

由于ps的dynamic type是Rectangle,所以解析为Rectangle::draw(),由于ps的static type是Shape,所以解析为Rectangle::draw(Shape::Red)。

因此,应将带default parameter value的函数声明为non-virtual,其实现用一个private virtual function完成:

class  Shape {
public
:
  
enum
 ShapeColor { Red, Green, Blue };
  
void draw(ShapeColor color = Red) const           // now non-virtual

  {
    doDraw(color);                                  
// calls a virtual

  }
  ...

private
:
  
virtual void doDraw(ShapeColor color) const = 0;  // the actual work is

};                                                  // done in this func

class Rectangle: public  Shape {
public
:
  ...

private
:
  
virtual void doDraw(ShapeColor color) const;       // note lack of a

  ...                                                // default param val.
};

Things to Remember

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 36 - 37
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值