Leśny Rumcajs
Leśny Rumcajs

Reputation: 2516

final class and final member functions

Say I have the base class:

struct Base
{
    virtual void foo();
};

and the derived class is final struct A final : public Base. Does it make sense to make the member functions final as well? I've seen in several places e.g.

struct A final : public Base {
    void foo() final;
}

I am not sure it provides any value in this case as if the class itself is final I guess all the member functions are final by default as well. Am I missing something? Are there any guidelines?

Upvotes: 2

Views: 40

Answers (1)

Raz Rotenberg
Raz Rotenberg

Reputation: 609

In case a struct or a class (A in your case) is final, you cannot declare another one inheriting it. Therefore, there's no need to also declare any methods as final.

Maybe this is a convention in some places to be clear that this method also cannot be overridden (just as a "reminder" for the final of the struct).

Upvotes: 2

Related Questions