zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    大数据DDos检测——DDos攻击本质上是时间序列数据,t+1时刻的数据特点和t时刻强相关,因此用HMM或者CRF来做检测是必然! 和一个句子的分词算法CRF没有区别!
    什么是私有密钥密码技术——密钥加密算法采用同一把密钥进行加密和解密
    条件随机场——时间序列(句子单词序列也算),其特征函数必须要考虑前一刻的数据
    隐形马尔可夫模型——前向算法就是条件概率
    MySQL添加字段和修改字段的方法
    shell脚本操作mysql数据库
    mysql 如何修改、添加、删除表主键
    shell按行合并文件
    MySQL主键添加/删除
    MySQL 添加列,修改列,删除列
  • 原文地址:https://www.cnblogs.com/zoneofmine/p/15351293.html
Copyright © 2011-2022 走看看