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

Started by
5 comments, last by RonHiler 14 years, 5 months ago
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
Advertisement
Small mistake:

Class B derive from Class A and not from Class ... 1 .

Sorry
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).
Thanks.

Can you give me the link from where you get those quote, i did not find it whilr googling for C++ standard


Thanks
current draft
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.
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.
Creation is an act of sheer will

This topic is closed to new replies.

Advertisement