Effective C++ 笔记 —— Item 37: Never redefine a function's inherited default parameter value.

Virtual functions are dynamically bound, but default parameter values are statically bound.

An object’s static type is the type you declare it to have in the program text. Consider this class hierarchy:

// 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;
        // ...
    };

    class Circle : public Shape 
    {
    public:
        virtual void draw(ShapeColor color) const;
        // ...
    };
};

Now consider these pointers:

Shape *ps; // static type = Shape*
Shape *pc = new Circle; // static type = Shape*
Shape *pr = new Rectangle; // static type = Shape*

 

An object's dynamic type is determined by the type of the object to which it currently refers. That is, its dynamic type indicates how it will behave. In the example above, pc’s dynamic type is Circle*, and pr's dynamic type is Rectangle*. As for ps, it doesn't really have a dynamic type, because it doesn't refer to any object (yet).

Dynamic types, as their name suggests, can change as a program runs, typically through assignments:

ps = pc; // ps’s dynamic type is now Circle*

ps = pr; // ps’s dynamic type is now Rectangle*

pc->draw(Shape::Red); // calls Circle::draw(Shape::Red)
pr->draw(Shape::Red); // calls Rectangle::draw(Shape::Red)

Virtual functions are dynamically bound, but default parameters are statically bound. That means you may end up invoking a virtual function defined in a derived class but using a default parameter value from a base class:

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

 

The fact that ps, pc, and pr are pointers is of no consequence in this matter. Were they references, the problem would persist. The only important things are that draw is a virtual function, and one of its default parameter values is redefined in a derived class.

 

Why does C++ insist on acting in this perverse manner? The answer has to do with runtime efficiency. If default parameter values were dynamically bound, compilers would have to come up with a way to determine the appropriate default value(s) for parameters of virtual functions at runtime, which would be slower and more complicated than the current mechanism of determining them during compilation. The decision was made to err on the side of speed and simplicity of implementation, and the result is that you now enjoy execution behavior that is efficient.

 

When you’re having trouble making a virtual function behave the way you'd like, it’s wise to consider alternative designs, and Item 35 is filled with alternatives to virtual functions. One of the alternatives is the non-virtual interface idiom (NVI idiom): having a public non-virtual function in a base class call a private virtual function that derived classes may redefine. Here, we have the non-virtual function specify the default parameter, while the virtual function does the actual work:

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

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 redefining — are dynamically bound.

 

posted @ 2021-09-29 09:34  MyCPlusPlus  阅读(76)  评论(0编辑  收藏  举报