This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) software development guidelines.
This diagnostic rule is relevant only for C++.
The analyzer has detected that an overridden virtual function specifies default arguments that differ from the values in the base class's function. Such code may contain an error. The issue is that the default arguments are selected at compile time, while the virtual function is selected at runtime. As a result, a polymorphic call via the base class leads to unexpected behavior: the implementation from the derived class is executed, but with the default values from the parent class.
The example:
class Base
{
public:
virtual void print(int value = 10);
};
class Derived : public Base
{
public:
void print(int value = 20) override;
};
void foo(Base* b)
{
b->print();
}
In the base class, the value parameter of the print function has a default value of 10, while in the derived class, it is 20. When the function is called via a pointer to the Base* base class, the compiler substitutes the value 10 but executes the code of the Derived::print function. To fix the error, either remove all of the default arguments from the overriding function or set them to the same values as in the base class.
The fixed code:
class Base
{
public:
virtual void print(int value = 10);
};
class Derived : public Base
{
public:
void print(int value) override;
};
void foo(Base* b)
{
b->print();
}
Was this page helpful?
Your message has been sent. We will email you at
If you do not see the email in your inbox, please check if it is filtered to one of the following folders: