If I have the code:
struct Parent
{
virtual void fn();
};
struct Child : public Parent
{
virtual void fn() override final
{
Parent::fn();
}
};
is there a way to have Parent::fn be final only when accessed through Child without re-implementing fn, so that some other class can override fn when deriving from Parent but not when deriving from Child?
like:
struct Child : public Parent
{
virtual void fn() override final = Parent::fn;
};
or some other syntax?
No, you can't do it without reimplementing it. So just reimplement it:
struct Child : public Parent
{
virtual void fn() override final { Parent::fn(); }
};
N.B. saying virtual ... override final is entirely redundant, final is an error on a non-virtual function, so you should just say:
void fn() final { Parent::fn(); }
See http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-override
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With