Incompatible types prevent overriding
Derived class method hides a virtual
base
class method instead of overriding it
Description
This defect occurs
when a derived class method has the same name and number of parameters
as a virtual
base class method but:
The parameter lists differ in at least one parameter type.
The parameter lists differ in the presence or absence of qualifiers such as
const
.
The derived class method hides the virtual
base
class method instead of overriding it.
Risk
You might inadvertently hide the base class method instead of overriding it with the derived class method.
If the base class method is hidden and you use a derived class object to call the method with the base class parameters, the derived class method is called instead. For the parameters whose types do not match the arguments that you pass, a cast takes place if possible. Otherwise, a compilation failure occurs.
Fix
To override a base class virtual method with a derived class method, declare the methods by
using identical parameter lists. For instance, change the parameter type or add a
const
qualifier if required.
In C++11 and later, you can declare intended overriding methods in the derived
class by using the specifier override
. When you declare the
derived class methods by using the specifier override
, the
compilation fails if the parameter lists of the base class method and the derived
class method are different. The derived class methods cannot hide base class methods
inadvertently and overriding of the base class virtual methods is ensured.
Otherwise, add the line using
to the derived class declaration. You can then access the base class method using an
object of the derived class.Base_class_name
::method_name
Examples
Result Information
Group: Object oriented |
Language: C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax: VIRTUAL_FUNC_HIDING |
Impact: Medium |
Version History
Introduced in R2015b
See Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)