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

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

提交回复
热议问题