C++ “virtual” keyword for functions in derived classes. Is it necessary?

后端 未结 9 1417
遇见更好的自我
遇见更好的自我 2020-11-22 15:17

With the struct definition given below...

struct A {
    virtual void hello() = 0;
};

Approach #1:

struct B : public A {
           


        
9条回答
  •  失恋的感觉
    2020-11-22 15:37

    I will certainly include the Virtual keyword for the child class, because

    • i. Readability.
    • ii. This child class my be derived further down, you don't want the constructor of the further derived class to call this virtual function.

提交回复
热议问题