Main Content

AUTOSAR C++14 Rule A10-2-1

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

Description

Rule Definition

Non-virtual public or protected member functions shall not be redefined in derived classes.

Rationale

When a nonvirtual public or protected member function is redefined in a derived class, the new definition in the derived class hides the definition in the base class instead of overriding it. When functions are hidden in the derived class, you cannot implement a common interface to handle different classes of the same hierarchy, resulting in unnecessary complexity and error. Such behavior might be unexpected and lead to bugs that are difficult to resolve.

Redefinitions of functions from private inheritance or functions that are private in the base class are not affected by this rule.

Polyspace Implementation

Polyspace® flags redefinitions of non-virtual member functions in a derived class. Polyspace does not raise this defect on destructors.

To justify a redefinition that you deem as acceptable, use annotations. See Annotate Code and Hide Known or Acceptable Results

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

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 {} //Noncompliant    
    void G() noexcept override {} //Compliant 
};

In this example, the A::F() function is a non-virtual public member function that is hidden by the B class. Hiding A::F() prevents the use of polymorphic interfaces, so Polyspace flags the redefinition in B::F() as noncompliant. The A::G() function is virtual and is overridden (rather than hidden) in B::G(), so Polyspace does not flag this implementation as noncompliant.

Check Information

Group: Derived Classes
Category: Required, Automated

Version History

Introduced in R2019a