Part8 多态性 8.4override与final

多态行为的基础:基类声明虚函数,继承类声明一个函数覆盖该虚函数
覆盖要求: 函数签名(signatture)完全一致
函数签名包括:函数名 参数列表 const

//下列程序就仅仅因为疏忽漏写了const,导致多态行为没有如期进行
#include<iostream>
using namespace std;
class Base{
public:
    virtual void f1(int) const;
    virtual ~Base(){};
};
void Base::f1(int) const{
    cout << "Base f1" << endl;
    return;
}

class Derived:public Base{
public:
    void f1(int);
    ~Derived(){};
};
void Derived::f1(int){
    cout << "derived f1" << endl;
}

int main(){
    Base *b;
    b = new Base;
    b->f1(1);
    b = new Derived;
    b->f1(1);
    return 0;
}

//运行结果:
//Base f1
//Base f1


显式函数覆盖
  C++11 引入显式函数覆盖,在编译期而非运行期捕获此类错误。
  在虚函数显式重载中运用,编译器会检查基类是否存在一虚拟函数,
  与派生类中带有声明override的虚拟函数,有相同的函数签名(signature);若不存在,则会回报错误。

final
  C++11 提供的final,用来避免类被继承,或是基类的函数被改写

    struct Base1 final { };
    struct Derived1 : Base1 { }; // 编译错误:Base1为final,不允许被继承

    struct Base2 { virtual void f() final; };
    struct Derived2 : Base2 { void f(); // 编译错误:Base2::f 为final,不允许被覆盖 ;

 

posted @ 2017-12-26 15:23  LeoSirius  阅读(139)  评论(0编辑  收藏  举报