AutosarC++19_03-A10.2.1

Non-virtual public or protected 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.

Note that a maintenance change to a private implementation detail could impact clients of the base class, and often it will be the case that those clients may not be in a position to fix the problem. Therefore, redefinitions of functions which are private in the base class are not affected by this rule.

Exceptions

Redefinition of functions from private inheritance do not violate this rule.

Example
// $Id: A10-2-1.cpp 317123 2018-04-23 08:48:11Z ilya.burylov $
    class A
    {
       public:
         virtual ~A() = default;
         void F() noexcept {}
         virtual void G() noexcept {}
       private:
         void H() noexcept {}
    };
    class B : public A
    {
       public:
         void
         F() noexcept {} // Non-compliant - F() function from A class hidden by Bclass
         void G() noexcept override {} // Compliant - G() function from A class
                                       // overridden by B class
       private:
         void H() noexcept {} // Compliant - H() function is private in A class
    };
    class C : private A
    {
       public:
         F() noexcept {} // Compliant by exception - private inheritance
    };
    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 [8]: AV Rule 94 An inherited nonvirtual function shall not be redefined in a derived class.
  • C++ Core Guidelines [11]: 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 = True

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