Sign in to follow this  

About the good usage of the "virtual" keyword in C++

This topic is 2956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. A few month age i was writting test application to clearly understant the virtual destructor call order. While doing those test, i notice that for virtual destructor, the keyword "virtual" was only necessary at the highest level of the hierarchy: class A { public: virtual ~A(); <-This one is virtual } class B : public 1 { public: ~B(); <-This one is also virtual } class C : public B { public: ~C(); <-This one is also virtual } Until now i thaught that the fact of omiting the "vitual" keyword somewhere in the derivation chain would "break" virtuality of the method.... So my questions are: How do we exactly use the "virtual" keyword for destructor AND classic methods. Is it necessary to specify it ONLY at the highest level, or is it necessary to re specify it every time ?? Does this behavior is standardized or does it change over compiler ? Thanks for your help. Clement Vidal

Share this post


Link to post
Share on other sites
Quote:
Original post by ClementLuminy
How do we exactly use the "virtual" keyword for destructor AND classic methods.
Is it necessary to specify it ONLY at the highest level, or is it necessary to re specify it every time ??

The former.

Quote:
Does this behavior is standardized or does it change over compiler ?

Standardized. (section 10.3.2 of the standard)
Quote:
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 (8.3.5), and cv-qualification as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf.
Quote:
The use of the virtual specifier in the declaration of an overriding function is legal but redundant (has empty semantics).

Share this post


Link to post
Share on other sites
There is a difference between doing stuff because it is required for the computer, and doing stuff because it is useful for the humans.


There are many common "best practices" that make no difference to the compiler. Just look at naming conventions, whitespace conventions, commenting conventions, to name a few.


The compiler only needs the virtual keyword at the highest level of the hierarchy, but it is a common practice to restate it in child classes.


Adding the redundant virtual keyword in derived classes is useful for the humans who read the code without looking all the parent/grandparent classes.

Share this post


Link to post
Share on other sites
I agree with frob. Just make ALL your destructors virtual. It doesn't hurt anything for the child classes, and if it's not a habit you get into, the one time you forget to do it on a top level class, it will bite you in the ass. And it won't be an easy bug to find.

Share this post


Link to post
Share on other sites

This topic is 2956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this