阅读背景:

Effective C++:绝不重新定义继承而来的缺省参数值

来源:互联网 

考虑下面这段程序:

#include <iostream>
using namespace std;

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

class Rectangle : public Shape {
public:
    virtual void draw(ShapeColor color = Green) const {
        cout << "Rectangle::draw, color = " << color << endl;
    }
};

class Circle : public Shape {
public:
    virtual void draw(ShapeColor color) const {
        cout << "Circle::draw, color = " << color << endl;
    }
};

int main()
{
    /*
    Circle circle;
    circle.draw();      //编译出错
    */

    /*
    Shape *pc = new Circle;
    pc->draw();         //color = 0(Red)
    */

    /*
    Rectangle rtg;
    rtg.draw();         //color = 1(Green)
    */

    /*
    Shape *pr = new Rectangle;
    pr->draw();         //color = 0(Red)
    */

    return 0;
}
#include <iostream>
using namesp



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: