Why is 'virtual' optional for overridden methods in derived classes?

前端 未结 5 1814
走了就别回头了
走了就别回头了 2021-02-05 12:53

When a method is declared as virtual in a class, its overrides in derived classes are automatically considered virtual as well, and the C++ language ma

5条回答
  •  情歌与酒
    2021-02-05 13:25

    Weak point in design, I agree. I also think that'd be really nice if there was a different syntax for two different things:

    1. Declaring a virtual function. I.e. the function that may be overridden in derived class. (This thing actually adds a new function entry in the vtable.)
    2. Overriding a virtual function in the derived class.

    With current C++ rules when overriding a function - it's easy to screw things. If you mistype the function name (or make a mistake in its parameters list) - then you actually do (1) instead of (2).

    And you have no error/warning. Just get the surprise at run-time.

提交回复
热议问题