C++11 之 override 关键字

Linux大全评论796 views阅读模式

1  公有继承

  当派生类公有继承自 (public inheritance) 基类时,继承包含两部分:一是函数的"接口" (interface),二是函数的"实现" (implementation)

  基类 Shape 中有三个不同形式的成员函数,分别代表公有继承的三种情况

class Shape {
public:
    virtual void draw() const = 0;
    virtual void error(const std::string& msg);
    int objectID() const;
};

class Rectangle: public Shape { ... };

class Ellipse: public Shape { ... };

1.1  纯虚函数

  虚函数末尾加上 "= 0",声明为纯虚函数 (pure virtual),表示派生类继承的只是基类成员函数的接口,且要在派生类中重写该函数的实现

  同时,一个基类中包含了纯虚函数,则该基类便为抽象基类,是不能被实例化的

Shape *ps = new Shape; // error! Shape is abstract

  如下代码,调用的是派生类 Rectangle 和 Ellipse 中,各自经过重写 (override) 的成员函数 Rectangel::Draw 和 Ellipse::Draw

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

  当然,如果想要调用基类的成员函数,可以加上类作用域操作符 ::

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

1.2  一般虚函数

  函数声明前加 virtual 关键字,且末尾没有 "= 0",则该函数即为一般虚函数,需要在基类中定义一个缺省的实现 (implementation)

  一般虚函数,表示派生类继承的是基类成员函数的接口和缺省的实现,且派生类可自行选择是否重写该函数的实现 (implementation)

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

class Airport { ... }; // represents airports

class Airplane {
public:
    virtual void fly(const Airport& destination);
};

void Airplane::fly(const Airport& destination)
{
    // default code for flying an airplane to the given destination
}

class ModelA: public Airplane { ... };
class ModelB: public Airplane { ... };

  这是典型的面向对象设计,两个类共享 (share) 一个共同的特性 (feature) -- fly,则 fly 可以在基类中实现,并由两个派生类公有继承之

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

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

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

Airport PDX(...); // PDX is the airport

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

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

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

class Airplane {
public:
    virtual void fly(const Airport& destination) = 0;
};

void Airplane::fly(const Airport& destination)
{ 
    // a pure virtual function default code for flying an airplane to the given destination
}

class ModelA: public Airplane {
public:
    virtual void fly(const Airport& destination) { Airplane::fly(destination); }
};

class ModelB: public Airplane {
public:
    virtual void fly(const Airport& destination) { Airplane::fly(destination); }
};

  这样在派生类 ModelC 中,即使一不小心忘记重写 fly 函数,则也不会调用 Airplane 的缺省实现,造成不必要的损失

class ModelC: public Airplane {
public:
    virtual void fly(const Airport& destination);
};

void ModelC::fly(const Airport& destination)
{
    // code for flying a ModelC airplane to the given destination
}

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

1.3  非虚成员函数

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

  既然继承的是一个强制的实现,那么此时就要求,在派生类中,不要重新定义 (redefine) 继承自基类的成员函数,如下所示:

class B {
public:
    void mf();
};

class D: public B { ... };

  使用指针调用 mf 函数,则都是调用的 B::mf()

D x; // x is an object of type D

B *pB = &x; // get pointer to x
pB->mf(); // call mf through pointer

D *pD = &x; // get pointer to x
pD->mf(); // call mf through pointer

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

class D: public B {
public:
    void mf(); // hides B::mf; see Item33
};

pB->mf(); // calls B::mf
pD->mf(); // calls D::mf

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

  这是因为非虚函数是“静态绑定”的,pB 被声明的是 B* 类型的指针,则通过 pB 调用的非虚函数都是基类 B 中的,既使 pB 指向的是派生类 D  与之“静态绑定”相对的是虚函数的“动态绑定”,即无论 pB 被声明为 B* 还是 D* 类型,其调用的虚函数取决于 pB 实际指向的对象类型

2  重写 (override)

  在 2.2 中提到,override 关键字可以避免,派生类中忘记重写虚函数的错误,下面以 Base 基类和 Derived 派生类为例,详细阐述之

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;
    virtual void mf2(int x) override;
    virtual void mf3() & override;
    void mf4() const override; // adding "virtual" is OK, but not necessary
}; 

 小结:

1)  public inheritance :

  pure virtual function => inheritance of interface

  impure virtual function => inheritance of interface plus default implementation

  non-virtual function => inheritance of interface plus madatory implementation

2)  never redefine an inherited non-virtual function

3)  declare overriding functions override

 

参考资料:

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

企鹅博客
  • 本文由 发表于 2019年9月8日 11:30:06
  • 转载请务必保留本文链接:https://www.qieseo.com/133626.html

发表评论