final
说明符 (C++11 起)
来自 cppreference.cn
目录 |
[编辑] 语法
当应用于成员函数时,标识符 final
出现在成员函数声明的语法中,或类定义内部的成员函数定义的 声明符 之后。
当应用于类(包括结构体和联合体)时,标识符 final
出现在类定义的开头,紧随类名之后,并且不能出现在类声明中。
declarator virt-specifier-seq (可选) pure-specifier (可选) | (1) | ||||||||
declarator virt-specifier-seq (可选) function-body | (2) | ||||||||
class-key attr (可选) class-head-name class-virt-specifier (可选) base-clause (可选) | (3) | (直到 C++26) | |||||||
class-key attr (可选) class-head-name class-prop-specifier-seq (可选) base-clause (可选) | (4) | (C++26 起) | |||||||
2) 在类定义内部的成员函数定义中,
final
可以出现在 virt-specifier-seq 中,紧随声明符之后,并且在 function-body 之前。3) 在类定义中,
final
可以作为 class-virt-specifier 出现在类名之后,紧接在开始 base-clause 的冒号之前(如果使用)。4) 在类定义中,
final
可以出现在 class-prop-specifier-seq 中(如果使用),但只能出现一次。在情况 (1,2) 中,virt-specifier-seq(如果使用)是 override
或 final
,或者 final override
或 override final
。在情况 (3) 中,class-virt-specifier 允许的唯一值(如果使用)是 final
。在情况 (4) 中,class-prop-specifier-seq(如果使用)可以包含任意数量的 类属性说明符 (C++26 起),但每个最多只能出现一次。
[编辑] 解释
当用于虚函数声明或定义时,final 说明符确保该函数是虚函数,并指定它不能被派生类覆盖。否则程序格式错误(会生成编译时错误)。
当用于类定义时,final 指定此类不能出现在另一个类定义的 base-specifier-list 中(换句话说,不能被派生)。否则程序格式错误(会生成编译时错误)。final 也可以与 union 定义一起使用,在这种情况下它没有效果(除了对 std::is_final 的结果有影响)(C++14 起),因为联合体不能被派生。
final 是一个标识符,当用于成员函数声明或类头时具有特殊含义。在其他上下文中,它不是保留字,可以用于命名对象和函数。
[编辑] 注
在以下 token 序列中
- 其中之一是 class、struct 和 union
- 一个可能是限定的 标识符
- final
- 其中之一是 : 和 {
序列中的第三个 token final 总是被视为说明符,而不是标识符。
运行此代码
struct A; struct A final {}; // OK, definition of struct A, // not value-initialization of variable final struct X { struct C { constexpr operator int() { return 5; } }; struct B final : C{}; // OK, definition of nested class B, // not declaration of a bit-field member final }; // Abnormal final usage. struct final final // OK, definition of a struct named `final` from which { // you cannot inherit }; // struct final final {}; // Error: redefinition of `struct final`, NOT a // definition of a variable `final` using an elaborated // type specifier `struct final` followed by an // aggregate initialization // struct override : final {}; // Error: cannot derive from final base type; // `override` in given context is a normal name void foo() { [[maybe_unused]] final final; // OK, declaration of a variable named `final` of type // `struct final` } struct final final; // OK, declaration of a variable named `final` of type // `struct final` using an elaborated type specifier int main() { }
[编辑] 关键词
[编辑] 示例
运行此代码
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
可能的输出
main.cpp:9:10: error: 'void A::bar()' marked 'final', but is not virtual 9 | void bar() final; // Error: bar cannot be final as it is non-virtual | ^~~ main.cpp:14:10: error: virtual function 'virtual void B::foo()' overriding final function 14 | void foo() override; // Error: foo cannot be overridden as it is final in A | ^~~ main.cpp:8:10: note: overridden function is 'virtual void A::foo()' 8 | void foo() final; // Base::foo is overridden and A::foo is the final override | ^~~ main.cpp:17:8: error: cannot derive from 'final' base 'B' in derived type 'C' 17 | struct C : B // Error: B is final |
[编辑] 参考文献
- C++23 标准 (ISO/IEC 14882:2024)
- 11 类 [class]
- 11.7.3 虚函数 [class.virtual]
- C++20 标准 (ISO/IEC 14882:2020)
- 11 类 [class]
- 11.7.2 虚函数 [class.virtual]
- C++17 标准 (ISO/IEC 14882:2017)
- 12 类 [class]
- 13.3 虚函数 [class.virtual]
- C++14 标准 (ISO/IEC 14882:2014)
- 9 类 [class]
- 10.3 虚函数 [class.virtual]
- C++11 标准 (ISO/IEC 14882:2011)
- 9 类 [class]
- 10.3 虚函数 [class.virtual]
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
CWG 1318 | C++11 | 一个类定义,在类名后有 final,并且 空的成员说明符列表可能会使 final 成为一个标识符 |
final 总是 在这种情况下是说明符 |
[编辑] 参阅
override 说明符 (C++11) |
显式声明一个方法覆盖另一个方法 |
类属性说明符 (C++26) | final 说明符 (C++11), 可替换性 (C++26), 平凡可重定位性 (C++26) |