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 theusing Base::fOverload
declaration introduces the functionfOverload(int32_t idx)
into the derived class, it does not introduce the functionfOverload(float f)
because that overload already exists in the derived class.The
using Base::fNoOverload
declaration introduces the functionfNoOverload(int32_t idx)
into the derived class. The functionfNoOverload(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
Check Information
Group: Basic concepts |
Category: Required |
Version History
Introduced in R2024b