Consider following code:
struct virtualfoo
{
virtualfoo{};
virtual ~virtualfoo{};
virtual double doStuff() = 0
};
struct realbar : virtualfoo
{
realbar{};
virtual ~realbar{};
virtual double doStuff();
};
Since I want to implement doStuff()
for realbar
, virtual isn't mandatory. But if I get this right, it won't hurt to have the virtual specifier next to realbar::doStuff()
, does it?
What side effects could I get with using/not using virtual?
The virtual keyword is not necessary in the derived class. However it makes code clearer. Also in C++11 override
keyword is introduced which allows the source code to clearly specify that a member function is intended to override a base class method.
With keyword override
the compiler will check the base class(es) to see if there is a virtual function with this exact signature. And if there is not, the compiler will throws an error.