zoukankan      html  css  js  c++  java
  • C++11 之 override

    1  公有继承

      公有继承包含两部分:一是 "函数接口" (interface),二是 "函数实现" (implementation)

      如 Shape 类中,三个成员函数,对应三种继承方式:

    class Shape {
    public:
        virtual void Draw() const = 0;    // 1) 纯虚函数
        virtual void Error(const string& msg);  // 2) 普通虚函数
        int ObjectID() const;  // 3) 非虚函数
    };
    
    class Rectangle: public Shape { ... };
    class Ellipse: public Shape { ... };

    1.1  纯虚函数 (pure virtual)

      纯虚函数,继承的是基类中,成员函数的接口,且要在派生类中,重写成员函数的实现

    Shape *ps1 = new Rectangle;
    ps1->Draw(); // calls Rectangle::Draw
    
    Shape *ps2 = new Ellipse;
    ps2->Draw(); // calls Ellipse::Draw

      调用基类的 Draw(),须加 类作用域操作符 ::

    ps1->Shape::Draw(); // calls Shape::draw
    

    1.2  普通虚函数

      普通虚函数,会在基类中,定义一个缺省的实现 (default implementation),表示继承的是基类成员函数接口缺省实现,由派生类选择是否重写该函数

      实际上,允许普通虚函数 同时继承接口和缺省实现是危险的 如下, ModelA 和 ModelB 是 Airplane 的两种飞机类型,且二者的飞行方式完全相同

    class Airplane {
    public:
        virtual void Fly(const string& destination);
    };
    class ModelA: public Airplane { ... };
    class ModelB: public Airplane { ... };

      这是典型的面向对象设计,两个类共享一个特性 -- Fly,则 Fly 可在基类中实现,并由两个派生类继承之

      现增加另一个飞机型号 ModelC,其飞行方式与 ModelA,ModelB 不相同,如果不小心忘记在 ModelC 中重写新的 Fly 函数

    class ModelC: public Airplane {
        ... // no fly function is declared
    };

      则调用 ModelC 中的 fly 函数,就是调用 Airplane::Fly,但是 ModelC 的飞行方式和缺省的并不相同

    Airplane *pa = new ModelC;
    pa->Fly(Qingdao); // calls Airplane::fly!

      即前面所说的,普通虚函数同时继承接口和缺省实现是危险的最好是基类中实现缺省行为 (behavior),但只有在派生类要求时才提供该缺省行为

    1.2.1  方法一 

      一种方法是 纯虚函数 + 缺省实现,因为是纯虚函数,所以只有接口被继承,其缺省的实现不会被继承。派生类要想使用该缺省的实现,必须显式的调用

    class Airplane {
    public:
        virtual void Fly(const string& destination) = 0;
    };
    
    void Airplane::Fly(const string& destination)
    { 
        // a pure virtual function default code for flying an airplane to the given destination
    }
    
    class ModelA: public Airplane {
    public:
        virtual void Fly(const string& destination) { Airplane::Fly(destination); }
    };
    

      这样在派生类 ModelC 中,即使一不小心忘记重写 Fly 函数,也不会调用 Airplane 的缺省实现

    class ModelC: public Airplane {
    public:
        virtual void Fly(const string& destination);
    };
    
    void ModelC::Fly(const string& destination)
    {
        // code for flying a ModelC airplane to the given destination
    }

    1.2.2  方法二 

      可以看到,上面问题的关键就在于,一不小心在派生类 ModelC 中忘记重写 fly 函数,C++11 中使用关键字 override,可以避免这样的“一不小心”

    1.3  非虚函数

      非虚成员函数没有 virtual 关键字,表示派生类不但继承了接口,而且继承了一个强制实现 (mandatory implementation)

      既然继承了一个强制的实现,则在派生类中,无须重新定义 (redefine) 继承自基类的成员函数,如下:

      使用指针调用 ObjectID 函数,则都是调用的 Shape::ObjectID()

    Rectangel rc; // rc is an object of type Rectangle
    
    Shape *pB = &rc; // get pointer to rc
    pB->ObjectID(); // call ObjectID() through pointer
    
    Rectangle *pD = &rc; // get pointer to rc
    pD->ObjectID(); // call ObjectID() through pointer

      如果在派生类中重新定义了继承自基类的成员函数 ObjectID 呢?

    class Rectangel : public Shape {
    public:
        int ObjectID() const; // hides Shape::ObjectID
    };
    
    pB->ObjectID(); // calls Shape::ObjectID()
    pD->ObjectID(); // calls Rectagle::ObjectID()

      此时,派生类中重新定义的成员函数会 “隐藏” (hide) 继承自基类的成员函数

      这是因为非虚函数是 “静态绑定” 的,pB 被声明的是 Shape* 类型的指针,则通过 pB 调用的非虚函数都是基类中的,既使 pB 指向的是派生类

      与“静态绑定”相对的是虚函数的“动态绑定”,即无论 pB 被声明为 Shape* 还是 Rectangle* 类型,其调用的虚函数取决于 pB 实际指向的对象类型

     2  重写 (override)

      在 1.2.2 中提到 override 关键字,可以避免派生类中忘记重写虚函数的错误

      下面以重写虚函数时,容易犯的四个错误为例,详细阐述之

    class Base {
    public:
        virtual void mf1() const;
        virtual void mf2(int x);
        virtual void mf3() &;
        void mf4() const;    // is not declared virtual in Base
    };
    
    class Derived: public Base {
    public:
        virtual void mf1();        // declared const in Base, but not in Derived.
        virtual void mf2(unsigned int x);    // takes an int in Base, but an unsigned int in Derived
        virtual void mf3() &&;    // is lvalue-qualified in Base, but rvalue-qualified in Derived.
        void mf4() const;        
    };

      在派生类中,重写 (override) 继承自基类成员函数的实现 (implementation) 时,要满足如下条件:

      一虚:基类中,成员函数声明为虚拟的 (virtual)

      二容:基类和派生类中,成员函数的返回类型异常规格 (exception specification) 必须兼容

      四同:基类和派生类中,成员函数名形参类型常量属性 (constness) 和 引用限定符 (reference qualifier) 必须完全相同

      如此多的限制条件,导致了虚函数重写如上述代码,极容易因为一个不小心而出错

      C++11 中的 override 关键字,可以显式的在派生类中声明,哪些成员函数需要被重写,如果没被重写,则编译器会报错。

    class Derived: public Base {
    public:
        virtual void mf1() override;
        virtual void mf2(unsigned int x) override;
        virtual void mf3() && override;
        virtual void mf4() const override;
    };

      这样,即使不小心漏写了虚函数重写的某个苛刻条件,也可以通过编译器的报错,快速改正错误

    class Derived: public Base {
    public:
        virtual void mf1() const override;  // adding "virtual" is OK, but not necessary
        virtual void mf2(int x) override;
        void mf3() & override;
        void mf4() const override; 
    }; 

    小结:

    1)  公有继承

      纯虚函数      => 继承的是:接口 (interface)

      普通虚函数   => 继承的是:接口 + 缺省实现 (default implementation)

      非虚成员函数 =>继承的是:接口 + 强制实现 (mandatory implementation)

    2)  不要重新定义一个继承自基类的非虚函数 (never redefine an inherited non-virtual function)

    3)  在声明需要重写的函数后,加关键字 override

    参考资料:

     <Effective C++_3rd> item 34, item 36

     <Effective Modern C++> item 12

  • 相关阅读:
    好吧,CSS3 3D transform变换,不过如此!
    Webpack基本使用(详解)
    「万字整理 」这里有一份Node.js入门指南和实践,请注意查收 ❤️
    怎么解决禅道启动服务mysqlzt时的端口失败
    怎么删掉xampp文件夹
    禅道Bug管理工具环境搭建
    svn报错:“Previous operation has not finished; run 'cleanup' if it was interrupted“ 的解决方法
    Fiddler教程
    各种数据库默认端口总结
    百度网盘破解版下载
  • 原文地址:https://www.cnblogs.com/xinxue/p/5471708.html
Copyright © 2011-2022 走看看