Repeating virtual keyword

Hi!

Thanks for all the great help you give here!

My question is if I make a class which inherits from an abstract class
some function

class example
virtual void function() =0;

then it seems to compile whether I have:

class example_one : public example
virtual void function() ={...};

or

class example_two : public example
void function(){....};


so is there a difference/need to repeat the virtual keyword in derived classes?

Hopefully this still made sense without me adding all my confusing code :)
Hi Swizzler,

There is no need to use the virtual keyword in derived classes for pure virtual functions, in fact my compiler complains when I did it.

Cheers TheIdeasMans
Thank you for the quick answer!
thats quite alright, not quite sure why I am still up at 5am Sunday !!

TheIdeasMan
Ha ha, wow 5AM and still helping people out.... :)
@ Swizzler: do you know what the virtual keyword does in the first place?

It lets the compiler know it needs to create a virtual table for the function(s) you mark. This has to do with polymorphism -- the ability to use a common base class as if it was any of its derived classes -- which is an abstraction meant to help you design your program.

So you only need to use virtual when you need it. In your case, by adding it, your example_one class has a virtual table that you don't need.

http://en.wikipedia.org/wiki/Virtual_function
Both ways should compile and give the same result. If the function is virtual in the base class it is automatically virtual in the derived classes.
The keyword virtual has no any symantic sense for derived classes.
Topic archived. No new replies allowed.