jerry
jerry

Reputation: 355

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

I know the title's statement is true.

What 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's foo? In general, if your base class has a virtual function, the derived class's corresponding function is automatically virtual? Is this true?

Upvotes: 3

Views: 294

Answers (2)

Andrew Tomazos
Andrew Tomazos

Reputation: 68598

C++ 2011: 10.3 Virtual Functions

2: 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, parameter-type-list, cv-qualification, and ref-qualifier (or absence of same) as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) ...

Upvotes: 4

John Zwinck
John Zwinck

Reputation: 249123

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

Upvotes: 4

Related Questions