When a derived class overrides a virtual function, it may, but is not required to, repeat the virtual
keyword. Once a function is declared as virtual
, it remains virtual
in all the derived classes.
A derived-class function that overrides an inherited virtual function must have exactly the same parameter type(s) as the base-class function that it overrides.
With one exception, the return type of a virtual in the derived class also must match the return type of the function from the base class. The exception applies to virtuals that return a reference (or pointer) to types that are themselves related by inheritance. That is, if D
is derived from B
, then a base class virtual can return a B*
and the version in the derived can return a D*
. However, such return types require that the derived-to-base conversion from D
to B
is accessible. §15.5 (p. 613) covers how to determine whether a base class is accessible. We’ll see an example of this kind of virtual function in §15.8.1 (p. 633).