Consider the following code:
#include <iostream>
class B {
virtual void f() {
std::cout << "Base" << '\n';
}
};
class D final: public Base {
void f() final override {
std::cout << "Derived" << '\n';
}
};
Paying attention to the two uses of the final
contextual keyword above – available since C++11 – we can observe the following:
final
to D
's member function f()
prevents f()
from being overridden in a class derived from D
.final
to the class D
prevents it from further derivation. Therefore, it is not possible that the member function f()
is overridden by a class derived from D
, since such a derived class can't exist due to the final
applied to the class D
.
Is there any point in using final
as override control for a virtual
member function of a class declared as final
? Or is it merely redundant?
final
on a virtual
function in a final
derived class is redundant.
Just like saying virtual
on a method marked override
is redundant. C++ just is that way sometimes.