Specifies that a virtual function cannot be overridden in a derived class or that a class cannot be inherited from.

Syntax

The identifier final, if used, appears immediately after the declarator in the syntax of a member function declaration or a member function definition.

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-specifier-list(optional) (3)  
 
1) In a member function declaration, final may appear in virt-specifier-seq immediately after the declarator, and before the pure-specifier, if used.
2) In a member function definition, final may appear in virt-specifier-seq immediately after the declarator and just before function-body (which may begin with a member initializer list)
3) In a class definition, final may appear as class-virt-specifier immediately after the name of the class, just before the colon that begins the base-specifier-list, 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 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 (a compile-time error is generated) otherwise. final can also be used with a union definition, in which case it has no effect (other than on the outcome ofstd::is_final), 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

  1. struct Base
  2. {
  3. virtual void foo();
  4. };
  5.  
  6. struct A : Base
  7. {
  8. virtual void foo() final; // A::foo is final
  9. void bar() final; // Error: non-virtual function cannot be final
  10. };
  11.  
  12. struct B final : A // struct B is final
  13. {
  14. void foo(); // Error: foo cannot be overridden as it's final in A
  15. };
  16.  
  17. struct C : B // Error: B is final
  18. {
  19. };

See also

final specifier (since C++11)的更多相关文章

  1. 【final】站立会议---11.28

    名称:nice! 组长:李权 成员:韩媛媛 刘芳芳 宫丽君 于淼 项目名称:约跑app 时间:11月28日 12:30 内容: 新任务的分配 1.李权分配任务 2.韩媛媛写站立会议 3.刘芳芳修改BU ...

  2. 【final】站立会议---11.27

    名称:nice! 组长:李权 成员:于淼  刘芳芳韩媛媛 宫丽君 时间:11月27日 13:00 项目内容:约跑app(约吧) 地点:传媒西楼220室 内容: 新任务的分配 1.李权分配任务 2.韩媛 ...

  3. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  4. Google C++ 代码规范

    Google C++ Style Guide   Table of Contents Header Files Self-contained Headers The #define Guard For ...

  5. 【Cocos2d-x游戏开发】细数Cocos2d-x开发中那些常用的C++11知识

    自从Cocos2d-x3.0开始,Cocos2dx就正式的使用了C++11标准.C++11简洁方便的特性使程序的可拓展性和可维护性大大提高,也提高了代码的书写速度. 下面我们就来一起学习一下Cocos ...

  6. Java:final、static关键字 详解+两者结合使用

    一  final关键字 1) 关于final的重要知识点 final关键字可以用于成员变量.本地变量.方法以及类. final成员变量必须在声明的时候初始化或者在构造器中初始化,否则就会报编译错误. ...

  7. 你真的知道final关键字吗?

    概述 final在英文中是最终的,不可更改的.在Java中final修饰变量,函数和类,就像这个单词的意思,一旦使用赋值之后不可更改. final修饰的变量不可以被改变 finalTest类 publ ...

  8. [译] iOS 11.4.1 Beta:全新的USB限制模式

    (Source/原文链接 https://blog.elcomsoft.com/2018/06/ios-11-4-1-beta-usb-restricted-mode-has-arrived/) 作者 ...

  9. Final阶段第1周/共1周 Scrum立会报告+燃尽图 06

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2485] 版本控制:https://git.coding.net/liuyy08 ...

随机推荐

  1. C++ Primer : 第十三章 : 拷贝控制之拷贝、赋值与销毁

    拷贝构造函数 一个构造函数的第一个参数是自身类类型的引用,额外的参数(如果有)都有默认值,那么这个构造函数是拷贝构造函数.拷贝构造函数的第一个参数必须是一个引用类型. 合成的拷贝构造函数   在我们没 ...

  2. leetcode 39 Combination Sum --- java

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  3. poj1420 拓扑序

    题意:给出一个表格,一部分单元格是给定的数字,而另一部分单元格则是一个式子,表示是其他一些单元格的和,让你输出最后计算出的所有格子的数. 因为有些格子需要其他格子先计算出来,所以计算顺序是按照拓扑序的 ...

  4. 微信JS-SDK应用DEMO

    首先需要将以下函数写入TinkPHP的公用function.php文件中以便调用 // 基于ThinkPHP /** * php curl 请求链接 * 当$post_data为空时使用GET方式发送 ...

  5. 如何才能将Faster R-CNN训练起来?

    如何才能将Faster R-CNN训练起来? 首先进入 Faster RCNN 的官网啦,即:https://github.com/rbgirshick/py-faster-rcnn#installa ...

  6. C++ 实用的小程序

    1. 打开test_ids.txt 将里面的东西添加"1_",然后另存为test_ids_repaired.txt #include <iostream> #inclu ...

  7. ascii codec can't decode byte 0xe8 in position 0:ordinal not in range(128)

    问题描述:一个在Django框架下使用Python编写的定时更新项目,在一个Linux系统下运行没有问题,在另外一台Linux系统下测试,报如下错误: ascii codec can't decode ...

  8. soap的简单实现(PHP)

    1.非wsdl模式 (1)函数文件 testphp/ServiceFunctions.class.php <?php /** * @author 左小兵 * */ class ServiceFu ...

  9. MySQL-使用tcpdump排查MySQLl数据库tps飙升的问题

    可以直接输出 tcpdump -i eth1 -s -l - | strings | perl -e ' while(<>) { chomp; next if /^[^ ]+[ ]*$/; ...

  10. data pump (数据抽取)测试

    背景介绍>利用db_link直接pump抽取,减少转储文件集. 前提:   授权>  grant create public database link,create database l ...