So I was looking through some code for a project trying to debug it, and I ran into a gap in my understanding. The basic question is, what is the purpose of the "virtual" keyword before overridden functions in the sub class.

bool flag=false;
do {
cout << endl << "We have 5 objects with numbers 0..4" << endl;
cout << "Enter object number to view information about it " << endl;
cout << "Enter any other number to quit " << endl;
char onum; // in fact, this is a character, not a number
// this allows user to enter letter and quit...
cin >> onum;

In all the base classes, such as circle, the type() function is overridden. why is the virtual keyword used?

To document the fact that the member function is virtual.

03-30-2012

somekid413

So it doesn't add or change functionality? Kind of just a prettier way to make a comment saying its virtual?

also, holy crap you're a fast responder

03-30-2012

laserlight

Yes.

04-01-2012

VirtualAce

Virtual does mean something very important.

Quote:

By placing the keyword virtual before a method declaration it says that when referring to an instance of a superclass by a pointer or reference to a base class that the correct implementation should be resolved at run time and that the "highest level" implementation should be used.

As well you must use virtual when declaring pure virtual methods. However note that many compilers adhering to the previous standard will not require virtual in the implementations in the derived classes. This, IMO, is bad mojo. I believe the new standard requires the virtual keyword in the declaration in the derived classes as well. If the method is declared as virtual I would also denote this in the derived class method to make the code simpler to read. Otherwise you force the dev to navigate all the way back to the base class to determine which methods are virtual and/or pure virtual.

04-01-2012

laserlight

Quote:

Originally Posted by VirtualAce

As of the new standard I believe the compiler forces the virtual to be there in the derived classes

The final draft of C++11 does not have such a requirement.

04-01-2012

VirtualAce

Nice. Perhaps it is Microsoft that is going to enforce this in their compiler. I thought I read that this was going to be enforced in the new standard....as it should be.

04-01-2012

laserlight

A warning would be more appropriate since this is not a new feature. If it is an error, then it means that existing valid code could break.

04-01-2012

VirtualAce

Yes a warning would be the ideal approach and the only one I would think a compiler dev team would take since an error, like you said, would break existing code. But I have never been able to simply migrate code from built with one compiler version to another version without some massaging. As long as existing code is not recompiled on the new version it won't break. I remember migrating from 2003 to 2008 and oh what a pain it was given all the 3rd party libraries that were involved that I did not have access to and could not recompile on the new version. Fun times.