Default argument on a virtual function
ID |
c.maintainability.virtual_function_default_argument |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
medium |
Resource |
Api Design |
Language |
C / C++ |
Description
This virtual function declares a default argument. Default arguments are resolved statically using the compile-time type of the expression, while the call itself dispatches dynamically — so calling through a base pointer or reference can silently use the base class’s default even though a derived override runs. Give the function no default, or make the caller pass the value explicitly.
Rationale
This virtual function declares a default argument. Default arguments are resolved statically using the compile-time type of the expression, while the call itself dispatches dynamically — so calling through a base pointer or reference can silently use the base class’s default even though a derived override runs. Give the function no default, or make the caller pass the value explicitly.
The following code illustrates the pattern detected by this rule:
struct Shape {
// FLAGGED: Default argument on a virtual function
virtual void draw(int color = 0);
};