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

前端 未结 4 2003
清歌不尽
清歌不尽 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:01

    The output ("E") behaves exactly as one would expect it to behave.

    The reason: The dynamic (i.e. runtime) type of that reference is E. You are doing a static upcast to D, but that does not change the actual type of the object of course.

    That's the very idea behind virtual methods and dynamic dispatches: You see the behavior of the type you were instantiating, which is E, in this case.

    0 讨论(0)
  • 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]

    0 讨论(0)
  • 2020-12-05 19:12

    Quick answer may be no, but correct answer is yes

    C++ doesn't know about function hiding, so overriding virtual function without virtual keyword marks that function virtual too.

    0 讨论(0)
  • 2020-12-05 19:13

    You are not creating any copy of the object of e and putting it in d. So d.foo() follows normal polymorphic behavior and calls derived class method. A method which is declared as virtual in the base class becomes automatically virtual in the derived class also.

    0 讨论(0)
提交回复
热议问题