Is there a way to mark the parent virtual function final from the child class without overriding it

If I have a code:

struct Parent
{
    virtual void fn();
};

struct Child : public Parent
{
    virtual void fn() override final
    {
        Parent::fn();
    }
};

There is a way to have Parent::fnbe finalonly when accessing through Childwithout re-implementation fn, so that some others classcan override fnon output Parent, but not on exit Child?

as:

struct Child : public Parent
{
    virtual void fn() override final = Parent::fn;
};

or any other syntax?

+4
source share
1 answer

No, you cannot do this without redefining it. So just override it:

struct Child : public Parent
{
    virtual void fn() override final { Parent::fn(); }
};

NB saying virtual ... override finalcompletely redundant finalis a mistake for a non-virtual function, so you should just say:

    void fn() final { Parent::fn(); }

. http://isocpp.imtqy.com/CppCoreGuidelines/CppCoreGuidelines#Rh-override

+10

Source: https://habr.com/ru/post/1676122/


All Articles