MISRA C++:2023 Rule 13.3.2
Parameters in an overriding virtual function shall not specify different default arguments
Since R2024b
Description
Parameters in an overriding virtual function shall not specify different default arguments. 1
Rationale
If the default parameter of a virtual function is different in an overriding function, then the function call uses different values when invoked using different classes of a hierarchy. Such behavior is unexpected for a class hierarchy. Consider this class hierarchy:
class Base{
public:
virtual foo(int x = 0);
//...
};
class Derived: public Base{
public:
virtual foo(int x = 1) override; //Noncompliant
};foo() has different default
parameters depending on whether base::foo() or
Derived::foo() is invoked. This behavior is unexpected and can
result in unexpected results.Polyspace Implementation
Polyspace® reports a violation of this rule if an overriding virtual function has a different default parameter compared to the virtual function it overrides. This includes situations where the overriding virtual function has a default parameter, but the base virtual function does not have a default parameter. Polyspace does not report violations when overriding virtual functions specify no default parameters.
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: Derived Classes |
| Category: Required |
PQL Name: std.misra_cpp_2023.R13_3_2 |
Version History
Introduced in R2024b1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.