TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C++ Coding Standard
Search

Rule:  INT#027Checked automatically with code checker

Synopsis:If you override one of the base class's virtual functions, then you shall use the "virtual" or "override" keywords
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
     int foo(float x) override { ... } // C++11
}

class derived2: public base
{
   public:
     virtual int foo(int x) { ... }  // won't compile!
     int foo(int x) override { ... }  // won't compile!
};