If your base class has a virtual destructor, your own destructor is automatically virtual

I know the title statement is true.

How about a regular function?

for example

class Father {

    virtual void foo() {...;}

}

class Son : public Father {

    void foo() {...;}

}

class GrandSon : public Son {

    void foo() {...;}

}

      

Can GrandSon override Son foo? In general, if your base class has a virtual function, is the corresponding function of the derived class automatically virtual? It's true?

+3


source to share


2 answers


Yes, in C ++ a derived class "inherits" the virtual aspect of all methods, not just destructors.



+4


source


C ++ 2011: 10.3 Virtual Functions



2: If a vf virtual member function is declared in Base and a Derived derived directly or indirectly from Base , a vf member function with the same name, list parameter, cv-qualifier, and ref-qualifier (or lack of the same ) like Base :: vf , then Derived :: vf is also virtual ( whether or not it is declared ) ...

+4


source







All Articles