What does the keyword virtual do when overriding a method? I\'m not using it and everything works fine.
Does every compiler behave the same in this
A virtual
method in the base class will cascade through the hierarchy, making every subclass method with the same signature also virtual
.
class Base{
public:
virtual void foo(){}
};
class Derived1 : public Base{
public:
virtual void foo(){} // fine, but 'virtual' is no needed
};
class Derived2 : public Base{
public:
void foo(){} // also fine, implicitly 'virtual'
};
I'd recommend writing the virtual
though, if for documentation purpose only.