使用最终类说明符时,最终函数说明符是否多余? [英] Is the final function specifier redundant when using the final class specifier?
问题描述
当类已经是 final
时,是否有任何理由将函数指定为 final
?还是冗余?
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?
};
这样说是一个很好的经验法则:首先让整个班级 final
,并且仅在需要从类派生和/或重写特定函数时才切换为制作单个函数 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?
推荐答案
这肯定是多余的,因为将整个类标记为 final
使得无法派生
It is definitely redundant because marking whole class as final
makes it impossible to derive from this class and therefore override anything.
9个类[class]
9 Classes [class]
- 如果某个类被class-virt-specifier标记为final,并且在基本子句
中出现为基本类型的子句(第10条),则程序格式错误。
因此,编译器甚至无需检查源于类的类 final
类实际上尝试覆盖或不覆盖任何东西。
So compiler won't even bother to check whether class deriving from final
class actually tries to override anything or not.
这篇关于使用最终类说明符时,最终函数说明符是否多余?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!