Difference between revisions of "cpp/language/final"
m (→Explanation: rev box for std::is_final) |
m (→See also: use dsc*) |
||
Line 51: | Line 51: | ||
===See also=== | ===See also=== | ||
− | + | {{dsc begin}} | |
+ | {{dsc inc | cpp/language/dsc override}} | ||
+ | {{dsc end}} | ||
{{langlinks|de|es|fr|it|ja|pt|ru|zh}} | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Revision as of 04:16, 2 October 2020
Specifies that a virtual function cannot be overridden in a derived class or that a class cannot be inherited from.
Contents |
Syntax
When applied to a member function, the identifier final
appears immediately after the declarator in the syntax of a member function declaration or a member function definition inside a class definition.
When applied to a class, the identifier final
appears at the beginning of the class definition, immediately after the name of the class.
declarator virt-specifier-seq(optional) pure-specifier(optional) | (1) | ||||||||
declarator virt-specifier-seq(optional) function-body | (2) | ||||||||
class-key attr(optional) class-head-name class-virt-specifier(optional) base-clause(optional) | (3) | ||||||||
final
may appear in virt-specifier-seq immediately after the declarator, and before the pure-specifier, if used.final
may appear in virt-specifier-seq immediately after the declarator and just before function-body.final
may appear as class-virt-specifier immediately after the name of the class, just before the colon that begins the base-clause, if used.In the cases (1,2), virt-specifier-seq, if used, is either override
or final
, or final override
or override final
. In the case (3), the only allowed value of class-virt-specifier, if used, is final
Explanation
When used in a virtual function declaration or definition, final
specifier ensures that the function is virtual and specifies that it may not be overridden by derived classes. The program is ill-formed (a compile-time error is generated) otherwise.
When used in a class definition, final
specifies that this class may not appear in the base-specifier-list of another class definition (in other words, cannot be derived from). The program is ill-formed otherwise (a compile-time error is generated). final
can also be used with a union definition, in which case it has no effect (other than on the outcome of std::is_final)(since C++14), since unions cannot be derived from.
final is an identifier with a special meaning when used in a member function declaration or class head. In other contexts it is not reserved and may be used to name objects and functions.
Example
struct Base { virtual void foo(); }; struct A : Base { void foo() final; // Base::foo is overridden and A::foo is the final override void bar() final; // Error: bar cannot be final as it is non-virtual }; struct B final : A // struct B is final { void foo() override; // Error: foo cannot be overridden as it is final in A }; struct C : B // Error: B is final { };
See also
override specifier(C++11)
|
explicitly declares that a method overrides another method |