Why is 'virtual' optional for overridden methods in derived classes?

Posted by squelart on Stack Overflow See other posts from Stack Overflow or by squelart
Published on 2010-06-03T07:19:13Z Indexed on 2010/06/03 7:24 UTC
Read the original article Hit count: 184

When a method is declared as virtual in a class, its overrides in derived classes are automatically considered virtual as well, and the C++ language makes this keyword virtual optional in this case:

class Base {
    virtual void f();
};
class Derived : public Base {
    void f(); // 'virtual' is optional but implied.
};

My question is: What is the rationale for making virtual optional?

I know that it is not absolutely necessary for the compiler to be told that, but I would think that developers would benefit if such a constraint was enforced by the compiler.

E.g., sometimes when I read others' code I wonder if a method is virtual and I have to track down its superclasses to determine that. And some coding standards (Google) make it a 'must' to put the virtual keyword in all subclasses.

© Stack Overflow or respective owner

Related posts about c++

Related posts about best-practices