if your base class has a virtual destructor, your own destructor is automatically virtual

荒凉一梦 提交于 2021-01-27 13:44:24

问题


I know the title's statement is true.

What about a regular function?

For example

class Father {

    virtual void foo() {...;}

}

class Son : public Father {

    void foo() {...;}

}

class GrandSon : public Son {

    void foo() {...;}

}

Can GrandSon override Son's foo? In general, if your base class has a virtual function, the derived class's corresponding function is automatically virtual? Is this true?


回答1:


Yes, in C++ a derived class "inherits" the virtual aspect of all methods--not just destructors.




回答2:


C++ 2011: 10.3 Virtual Functions

2: If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list, cv-qualification, and ref-qualifier (or absence of same) as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) ...



来源:https://stackoverflow.com/questions/9879820/if-your-base-class-has-a-virtual-destructor-your-own-destructor-is-automaticall

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!