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

struct Base
{
virtual void foo();
}; struct A : Base
{
virtual void foo() final; // A::foo is final
void bar() final; // Error: non-virtual function cannot be final
}; struct B final : A // struct B is final
{
void foo(); // Error: foo cannot be overridden as it's final in A
}; struct C : B // Error: B is final
{
};

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. Codeforces Round #310 (Div. 2) A B C

    A. Case of the Zeros and Ones time limit per test 1 second memory limit per test 256 megabytes input ...

  2. ZOJ 1090 The Circumference of the Circle

    原题链接 题目大意:已知三角形的三个顶点坐标,求其外接圆的周长. 解法:刚看到这道题时,马上拿出草稿纸画图,想推导出重心坐标,然后求出半径,再求周长.可是这个过程太复杂了,写到一半就没有兴致了,还是求 ...

  3. sqlserver 索引

    什么是索引 拿汉语字典的目录页(索引)打比方:正如汉语字典中的汉字按页存放一样,SQL Server中的数据记录也是按页存放的,每页容量一般为4K .为了加快查找的速度,汉语字(词)典一般都有按拼音. ...

  4. kinect在ros上的初步测试---17

    摘要: 原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 1.在使用本贴前必须先按照我的上一个博文正确在ubuntu上安装kinect驱动:http:// ...

  5. leetcode 144. Binary Tree Preorder Traversal ----- java

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  6. C/C++笔试题(很多)

    微软亚洲技术中心的面试题!!! .进程和线程的差别. 线程是指进程内的一个执行单元,也是进程内的可调度实体. 与进程的区别: (1)调度:线程作为调度和分配的基本单位,进程作为拥有资源的基本单位 (2 ...

  7. 常用的文件和目录操作命令【转vbird】

    cdpwdmkdirrmdirlscprmmvcattacnlmorelessheadtailtouch cd (变换目录) 我们知道vbird这个使用者的家目录是/home/vbird/,而root ...

  8. 20150911 for循环的用法以及小题目

    for: for(初始条件,条件表达式,状态改变) { 循环体 } //100块钱,买2元一只的圆珠笔,3块钱一个的尺子,5元一个的铅笔盒,每样至少一个,正好花光,请问有多少中画法. //一张纸0.0 ...

  9. Q3: Linked List Cycle II

    问题描述 Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...

  10. MvcPager2.0 中分页初始化失败的问题

    页面初始化时只有一页数据,或没数据时,出现分页控件初始化失败以及后续Ajax分页功能失效的问题, <div class="pagin"> <div class=& ...