Overriding a function just to call the overridden function from the base class without performing any other actions can be useless and
misleading.
There are cases when it is justified because redeclaring the function allows some side effects:
- Changing the visibility of the function in the derived class
- Preventing the base class function from being hidden by an overload added in the derived class (a using-declaration could have the same effect)
- To resolve ambiguities in cases of multiple inheritance
- To make an inherited function final
This rule raises an issue when an override not in one of the abovementioned situations only calls the overridden function, directly forwarding its
arguments.
Noncompliant code example
class Base {
public:
virtual void f();
};
class Derived : public Base {
public:
virtual void f() {
Base::f(); // Noncompliant
}
};
Compliant solution
class Base {
public:
virtual void f();
};
class Derived : public Base {
};
or
class Base {
public:
void f();
};
class Derived : public Base {
private: // Compliant: change of visibility
virtual void f() {
Base::f();
}
};
or
class Base {
public:
void f();
};
class Derived : public Base {
public:
void f(int i);
void f() { // Compliant: prevents hiding by f(int)
Base::f();
}
};
or
class Base {
public:
virtual void f();
};
class Derived : public Base {
public:
void f() final { // Compliant: cannot be overridden by derived classes
Base::f();
}
};