|
-
March 1st, 2003, 06:18 AM
#1
About virtual functions
Someone gave me a big doubt about virtual functions. Can some one remind me which is the correct way about this:
Code:
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.
Elrond
A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
-- George Steiner
-
March 1st, 2003, 06:33 AM
#2
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...
-
March 1st, 2003, 06:41 AM
#3
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!
Elrond
A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
-- George Steiner
-
March 1st, 2003, 06:45 AM
#4
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 1st, 2003, 11:34 AM
#5
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
-
March 1st, 2003, 11:53 AM
#6
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.
Elrond
A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
-- George Steiner
-
March 1st, 2003, 11:55 AM
#7
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...
-
March 1st, 2003, 12:29 PM
#8
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|