Re: once virtual, always virtual?
Quote:
Originally Posted by kempofighter
I understand that it is preference. I'm just curious as to *why* it is a preference and why it would be part of a coding standard. I've worked in a lot of large organizations where people code like this and it seems harder to understand the code. You end up having to chase the first declaration of a function up the inheritance ladder until you find the first declaration in order to verify that a particular function is virtual. I wasn't trying to insult your preference; I'm trying to understand the reasoning of this preference. So again; if anyone has any ideas on that, I'd appreciate it. Sometimes we just follow guidelines without knowing why. In this case, I don't like the guideline, personally, but if there is a good reason to use it I'd like to know what it is.
Best Regards,
Shawn
You know it's virtual because of the comment "From foo" and the enclosing doxygen braces, plus the fact that foo appears in the base class list for that class, so there is no "chasing around" to find out whether it's virtual - you know, and you know where it came from. The virtual keyword in this case is redundant, and I simply prefer to leave it off - YMMV. Note also that this style groups related functions (i.e. those from the same base class) together in the generated doxygen documentation, whereas not doing this could leave them spread out according to alphabetical order.
Compare that to:
Code:
class bar : public foo
{
public:
// ...
virtual void f();
};
does f() come from foo or has it been introduced by bar? What has the "virtual" added to your understanding that a well-placed comment couldn't do more effectively? And if you add the comment, why do you need the "virtual" - it's redundant.
Add the "virtual" if you wish, but a comment that says "the following functions are overrides of virtuals from base class X" is, to my mind, much clearer and more useful, and it reduces the amount of "chasing around header files" that you have to do.