In C++, is a function automatically virtual if it overrides a virtual function?

前端 未结 4 2023
清歌不尽
清歌不尽 2020-12-05 18:12

I would expect that if foo is declared in class D, but not marked virtual, then the following code would call the implementation of foo

4条回答
  •  情歌与酒
    2020-12-05 19:02

    Standard 10.3.2 (class.virtual) says:

    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 and same parameter list as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides*

    [Footnote: A function with the same name but a different parameter list (clause over) as a virtual function is not necessarily virtual and does not override. The use of the virtual specifier in the declaration of an overriding function is legal but redundant (has empty semantics). Access control (clause class.access) is not considered in determining overriding. --- end foonote]

提交回复
热议问题