Synopsis: | If you override one of the base class's virtual functions, then you shall use the "override" or "final" keyword |
Language: | C++ |
Severity Level: | 2 |
Category: | Class Interface |
Description: |
It is not possible to remove the virtual-ness of an inherited virtual function: any virtual function overrides itself. This rule makes explicit that the function that overrides a virtual function is virtual too, which it would also have been if the overriding function were not declared as such. By adding the "override" keyword you express your intention that this function is an override of a function in the parent class. This is also checked by the compiler. If you make a mistake (see example below) the compiler will issue an error to indicate that the function doesn't override another function: class base { public: virtual int foo(float x) = 0; }; class derived: public base { public: virtual int foo(float x) { ... } // before C++11, not accepted any more int foo(float x) override { ... } // C++11 } class derived2: public base { public: int foo(int x) override { ... } // won't compile! }; |