析构函数可以是最终的吗?
May a destructor be final?
C++ 标准是否允许将析构函数声明为 final
?像这样:
class Derived: public Base
{
...
virtual ~Derived() final;
}
如果是这样,是否会阻止派生的声明 class:
class FurtherDerived: public Derived {// allowed?
}
如果允许,编译器是否会发出警告?将析构函数声明为 final
是否是指示 class 不打算用作基础 class 的可行习语?
(有no point in doing this in a ultimate base class,只有一个派生class。)
May a C++ destructor be declared as final
?
是的。
And if so, does that prevent declaration of a derived class:
是的,因为派生的 class 必须声明一个析构函数(由您显式声明或由编译器隐式声明),并且该析构函数将覆盖声明的 final
函数,即格式错误。
If a virtual function f
in some class B is marked with the virt-specifier final
and in a class D derived from B a function D::f
overrides B::f
, the program is ill-formed.
推导本身是病式的,不必使用它。
Is declaring a destructor to be final a workable idiom for indicating that a class is not intended to be used as a base class?
有效,但你应该只标记class final
。它更明确一些。
C++ 标准是否允许将析构函数声明为 final
?像这样:
class Derived: public Base
{
...
virtual ~Derived() final;
}
如果是这样,是否会阻止派生的声明 class:
class FurtherDerived: public Derived {// allowed?
}
如果允许,编译器是否会发出警告?将析构函数声明为 final
是否是指示 class 不打算用作基础 class 的可行习语?
(有no point in doing this in a ultimate base class,只有一个派生class。)
May a C++ destructor be declared as
final
?
是的。
And if so, does that prevent declaration of a derived class:
是的,因为派生的 class 必须声明一个析构函数(由您显式声明或由编译器隐式声明),并且该析构函数将覆盖声明的 final
函数,即格式错误。
If a virtual function
f
in some class B is marked with the virt-specifierfinal
and in a class D derived from B a functionD::f
overridesB::f
, the program is ill-formed.
推导本身是病式的,不必使用它。
Is declaring a destructor to be final a workable idiom for indicating that a class is not intended to be used as a base class?
有效,但你应该只标记class final
。它更明确一些。