override关键字

函数名后加上override,表明该函数重写了父类的虚函数,同时编译器在编译时会进行检查是否正确重写。

建议重写时加上该关键字,方便检查及减少bug。

class A {
public:
    virtual void f() {
        cout << "this is A" << endl;
    }
};

class B : public A {
public:
    void F() override {                    //报错,提示F()并没有重写
        cout << "this is B" << endl;
    }
};

 

posted @ 2022-02-26 19:28  雪下牧原  阅读(210)  评论(0)    收藏  举报