读书笔记_Effective_C++_条款三十七:绝不重新定义继承而来的缺省参数值

先看下面的例子:

 1 enum MyColor
 2 {
 3     RED,
 4     GREEN,
 5     BLUE,
 6 };
 7 
 8 class Shape
 9 {
10 public:
11     void virtual Draw(MyColor color = RED) const = 0;
12 };
13 
14 class Rectangle: public Shape
15 {
16 public:
17     void Draw(MyColor color = GREEN) const
18     {
19         cout << "default color = " << color << endl;
20     }
21 };
22 
23 class Triangle : public Shape
24 {
25 public:
26     void Draw(MyColor color = BLUE) const
27     {
28         cout << "default color = " << color << endl;
29     }
30 };
31 
32 
33 int main()
34 {
35     Shape *sr = new Rectangle();
36     Shape *st = new Triangle();
37     cout << "sr->Draw() = "; //
38     sr->Draw();
39     cout << "st->Draw() = "; //
40     st->Draw();
41     
42     delete sr;
43     delete st;
44 }

问号所在处的输出是什么?

要回答这个问题,需要回顾一下虚函数的知识,如果父类中存在有虚函数,那么编译器便会为之生成虚表与虚指针,在程序运行时,根据虚指针的指向,来决定调用哪个虚函数,这称之与动态绑定,与之相对的是静态绑定,静态绑定在编译期就决定了。

实现动态绑定的代价是比较大的,所以编译器在函数参数这部分,并没有采用动态绑定的方式,也就是说,默认的形参是静态绑定的,它是编译期就决定下来了。

我们看下这两行代码,分析一下:

1 Shape *sr = new Rectangle();
2 Shape *st = new Triangle();

sr的静态类型是Shape*,动态类型才是Rectangle*,类似地,st的静态类型是Shape*,动态类型是Triangle*。这里没有带参数,所以使用的是默认的形参,即为静态的Shape::Draw()里面的缺省值RED,所以两个问题所在处的输出值都是0。

正因为编译器并没有对形参采用动态绑定,所以如果对继承而来的虚函数使用不同的缺省值,将会给读者带来极大的困惑,试想一下下面两行代码:

1 Shape *sr = new Rectangle(); // 默认值是RED
2 Rectangle *rr = new Rectangle(); // 默认值是GREEN

如果一定要为虚函数采用默认值,那么只要在父类中设定就可以了,可以借用条款35所说的NVI方法,像下面这样:

 1 class Shape
 2 {
 3 public:
 4     void DrawShape(MyColor color = RED)
 5     {
 6         Draw(color);
 7     }
 8 private:
 9     virtual void Draw(MyColor color) const = 0
10     {
11         cout << "Shape::Draw" << endl;
12     }
13 };
14 
15 class Rectangle: public Shape
16 {
17 private:
18     void Draw(MyColor color) const
19     {
20         cout << "Rectangle::Draw" << endl;
21     }
22 };
23 
24 class Triangle : public Shape
25 {
26 private:
27     void Draw(MyColor color) const
28     {
29         cout << "Triangle::Draw" << endl;
30     }
31 };
32 
33 
34 int main()
35 {
36     Shape *sr = new Rectangle();
37     Shape *st = new Triangle();
38     cout << "sr->DrawRectangle() = "; // Rectangle::Draw
39     sr->DrawShape();
40     cout << "st->DrawTriangle() = "; // Triangle::Draw
41     st->DrawShape();
42     delete sr;
43     delete st;
44 }

因为前面条款已经约定non-virtual函数不会被覆写,所以这样就不用担心在子类中出现定义不同缺省形参值的问题了。

最后总结一下:

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

原文地址:https://www.cnblogs.com/jerry19880126/p/3599621.html