Main Content

MISRA C++:2023 Rule 6.4.2

Derived classes shall not conceal functions that are inherited from their bases

Since R2024b

Description

Rule Definition

Derived classes shall not conceal functions that are inherited from their bases.

Rationale

A function in a derived class that conceals a function with the same name in a base class can cause these issues:

  • The compiler does not look up a function func() in any base class if a function with the same name exists in a derived class, even if the function in the base class is a better match for the current call. The call to the function in the derived class would not be what a developer expects.

  • A direct call to a function and a call to the same function a through a base class pointer can result in different functions being called.

These issues do not apply when a derived class inherits member functions privately because those class member functions are not accessible outside of the derived class.

A function or variable in a derived class conceals a function in a base class if they have the same name, except in these cases:

  • The base class function is a move or copy assignment operator.

  • The derived class inherits class members from the base class privately.

  • The derived class function overrides a virtual function from the base class.

  • The derived class uses the using keyword to introduce the base class function.

    Note that the using declaration introduces a base class function overload only if that same overload does not already exist in the derived class.

    For example, in this code snippet, fOverload(float f) in the derived class is noncompliant because it conceals a similar function in the base class. While the using Base::fOverload declaration introduces the function fOverload(int32_t idx) into the derived class, it does not introduce the function fOverload(float f) because that overload already exists in the derived class.

    The using Base::fNoOverload declaration introduces the function fNoOverload(int32_t idx) into the derived class. The function fNoOverload(float f) is compliant because it does not conceal a function with the same name and signature from the base class.

    class Base
    {
    public:
        void fOverload(int32_t idx);
        void fOverload(float f); // Concealed function
        void fNoOverload(int32_t idx);
    };
    
    class Derived : public Base
    {
    public:
        using Base::fOverload;
        void fOverload(float f); // Noncompliant
    
        using Base::fNoOverload;
        void fNoOverload(float f); // Compliant
    };

Polyspace Implementation

The coding rule checker reports a violation if a class declares a function with the same name as a function in any of its base classes, regardless of function signature, except if one of these is true:

  • The function is declared virtual in the base class and has an override in the derived class.

  • The base class function is introduced into the derived class with the using keyword.

  • The base class is inherited as private in the derived class.

  • The function is a special member function such as a constructor, a destructor, or a copy or move assignment operator.

If a derived class contains a non-virtual function that has the same name as a pure virtual function in the base class, Polyspace® reports a violation. Override pure virtual functions using virtual functions in the derived classes.

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

#include <iostream>

class Engine
{
public:
    void start()
    {
        // Start engine
    }
    void stop()
    {
        // Stop engine
    }
    void setPower(int horsepower)
    {
        // Set engine power
    }
};

class ElectricEngine : public Engine
{
public:
    // using Engine::setPower;
    void setPower(double kilowatts) // Noncompliant
    {
        // Set engine power
    }

    
    void setHybridMode(bool isOn)
    {
    }
};

In this example, Polyspace reports a violation for the function setPower(double kilowatts) in the derived class ElectricEngine because it conceals the function setPower(int horsepower) in the base class Engine. The concealed function is not called even in cases where it might be a better match based on the call context.

Uncommenting the line above the declaration of setPower(double kilowatts) in the derived class introduces the function setPower(int horsepower) from the base class into the derived class. In this case, the function setPower(double kilowatts) does not conceal the function from the base class and Polyspace does not report a violation.

Check Information

Group: Basic concepts
Category: Required

Version History

Introduced in R2024b