Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override virtual function in good style? [C++]

guys I know this question is very basic but I've met in few publications (websites, books) different style of override virtual function. What I mean is: if I have base class:

class Base
{
public:
virtual void f() = 0;
};

in some publications I saw that to override this some authors would just say:

void f();     

and some would still repeat the virtual keyword before void. Which form of overwriting is in good style? Thank you for your answers.

like image 888
There is nothing we can do Avatar asked Mar 14 '10 19:03

There is nothing we can do


2 Answers

This is purely a matter of taste. Some weak arguments can be made back and forth as to the self-documentation value of some styles versus the non-redundancy of others.

like image 189
bmargulies Avatar answered Oct 11 '22 12:10

bmargulies


It is not necessary to add the virtual keyword to a method you override in a subclass as this qualifier can not be removed by omitting it in subclass declarations.

It is however good practise to repeat the virtual keyword as it documents which derived functions are virtual in the base class.

This is also recommended by the 'High Integrity C++ Coding Standard Manual'.

which is linked in the C++ FAQ.

like image 36
MKroehnert Avatar answered Oct 11 '22 13:10

MKroehnert