AutosarC++18_03-A10.2.1

Non-virtual member functions shall not be redefined in derived classes

Required inputs: IR

Rationale

A non-virtual member function specifies an invariant over the hierarchy. It cannot be overridden in derived classes, but it can be hidden by a derived class member (data or function) with the same identifier. The effect of this hiding is to defeat polymorphism by causing an object to behave differently depending on which interface is used to manipulate it, resulting in unnecessary complexity and error.

Example
// $Id: A10-2-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
class A {
public:
  virtual ~A() = default;
  void F() noexcept {}
  virtual void G() noexcept {}
};

class B : public A {
public:
  void
  F() noexcept // Non-compliant - f() function from A class hidden by B class
  {}
  void G() noexcept override // Compliant - g() function from A class
                             // overridden by B class
  {}
};
void Fn1(A &object) noexcept {
  object.F(); // Calls f() function from A
  object.G(); // Calls g() function from B
}
void Fn2() noexcept {
  B b;
  Fn1(b);
}
See Also
  • JSF December 2005 [7]: AV Rule 94 An inherited nonvirtual function shall not be redefined in a derived class.
  • C++ Core Guidelines [10]: ES.12: Do not reuse names in nested scopes.

Possible Messages

Key

Text

Severity

Disabled

redefinition_of_nonvirtual_function

Redefinition/hiding of non-virtual function.

None

False

Options

exclude_private_inheritance

exclude_private_inheritance : bool = False

If set, redefinition of functions from private inheritance is not reported.