Rule Definition
An overridden virtual method should have the same default values as the base class method. Default parameter values are determined by the static type of the object on which the method call is done. This means that the default values used may not match those of the virtual method being called. For non virtual methods, this makes the code more complex to understand.
Remediation
Check the parameter default value and try to align them with those that are defined in the overridden method.
Violation Code Sample
class Base
{
public:
virtual void myfct( int a = 0 );
};
class Derived : public Base
{
public:
virtual void myfct( int a = 10 ); // VIOLATION
};
Fixed Code Sample
class Base
{
public:
virtual void myfct( int a = 0 );
};
class Derived : public Base
{
public:
virtual void myfct( int a = 0 ); // FIXED
};
Reference
Effective C++, Item 38;
Related Technologies
C++
Technical Criterion
Programming Practices - OO Inheritance and Polymorphism
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.