Click to See Complete Forum and Search --> : About virtual functions


Elrond
March 1st, 2003, 05:18 AM
Someone gave me a big doubt about virtual functions. Can some one remind me which is the correct way about this:


class Base
{
...
virtual void display() = 0;
...
};

class Derived : public Base
{
...
virtual void display();
...
};

Must we put the keyword "virtual" before the function display in Derived, or must we not put it, or can we do what we want.

Andreas Masur
March 1st, 2003, 05:33 AM
Originally posted by Elrond
Must we put the keyword "virtual" before the function display in Derived, or must we not put it, or can we do what we want.
You do not need to repeat the 'virtual' keyword in the derived class, however it will not fail if you do... :cool:

Elrond
March 1st, 2003, 05:41 AM
But in this case what is considered as "best practice". To put virtual or not to put it?

It's easy to check that it will work, but a lot of things work that are not really good practice, and some times not even well defined!

Graham
March 1st, 2003, 05:45 AM
I usually repeat the "virtual". It's redundant, but adds a bit of extra documentation, given that the base class is usually defined in a different file. Mind you, I wouldn't bother repeating the "pure" declaration if I intended to have the actual implementation in a further derived class, so maybe I'm inconsistent.

PaulWendt
March 1st, 2003, 10:34 AM
I'd like to say that I usually repeat the virtual as well. I've heard
a lot of people tell you NOT to since it can be confusing ... but I
find it's more confusing to have to look at one class and then go
through all of its ancestors to see whether or not the function is
virtual.

--Paul

Elrond
March 1st, 2003, 10:53 AM
Someone just gave me part of the standard saying that using the keyword virtual for derived classes is not an error as the derived classes are implicitely virtual. This really means that you can do whatever you want, and that it's just a question of coding practice.

I used to always repeat the virtual keyword, but someone told me it could be a mistake, so I suddenly had some doubts. This that work with a compiler might still be kind of wrong because the compiler is not standard.

I guess I'll keep using virtual then.

Thanks for your help.

Mick
March 1st, 2003, 10:55 AM
Originally posted by PaulWendt
I'd like to say that I usually repeat the virtual as well. I've heard
a lot of people tell you NOT to since it can be confusing ... but I
find it's more confusing to have to look at one class and then go
through all of its ancestors to see whether or not the function is
virtual.

--Paul


Ditto...

jfaust
March 1st, 2003, 11:29 AM
This is one of my biggest complaints with C++. I think omitting the virtual keyword should be a compile error, plain and simple.

This would
1) prevent you from mistakingly overriding a virtual method, and
2) make it much easier to determine what is virtual in any given class without searching through the hierarchy.

Jeff