Closed serg06 closed 4 years ago
virtual
should stay. final
must be coupled with a virtual member function. I think the confusion is from this code example in the page you link:
struct Base
{
virtual void foo();
};
struct A : Base
{
void foo() final; // Base::foo is overridden and A::foo is the final override
};
foo
is clearly not virtual because there is no virtual specifier -- or is it! Even though there is no virtual
keyword there, it's still virtual.
Then this function in the class Derived is also virtual (whether or not the keyword virtual is used in its declaration) and overrides Base::vf (whether or not the word override is used in its declaration).
Source: https://en.cppreference.com/w/cpp/language/virtual
f
.
struct A {
// void f() final; <- Compiler error: needs virtual
virtual void f() final; // OK
};
struct B : A {
void f(); // Compiler error: overrides final mf
};
The point is maybe I want derived classes to have f
as a member function without it being changed in a derived class.
Ahh I see. Thanks for clearing it up for me.
virtual final
kinda redundant? Why would you allow overriding withvirtual
, then disallow it withfinal
?