Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the final function specifier redundant when using the final class specifier?

Tags:

c++

c++11

Is there any reason to specify a function as final when the class is already final? Or is that redundant?

class B
{
public:
    virtual void f();
};

class D final : public B
{
public:
    virtual void f() final; // Redundant final?
};

Would it be a good rule of thumb to say: Start with making the whole class final, and only switch to making individual functions final when you need to derive from the class and/or override specific functions?

like image 211
Barnett Avatar asked Apr 30 '17 09:04

Barnett


1 Answers

It is definitely redundant because marking whole class as final makes it impossible to derive from this class and therefore override anything.

9 Classes [class]

  1. If a class is marked with the class-virt-specifier final and it appears as a base-type-specifier in a base-clause (Clause 10), the program is ill-formed.

So compiler won't even bother to check whether class deriving from final class actually tries to override anything or not.

like image 156
user7860670 Avatar answered Nov 13 '22 08:11

user7860670