当前位置: 首页 > news >正文

c++11 关键字 final 使用

写在最前。。。

请支持原创~~ 

1. 功能

  • 用以指定一个 virtual function 不能被派生类重写;
  • 或者指定一个 class 不能被继承;

2. 语法

对于类中成员函数有两种情况:

  • 只声明时,final 紧跟参数的右括号,如果是纯虚函数,final 会出现在 = 0 之前;
  • 类中定义时,final 在 函数体之前;

对于类定义,final 紧跟类名;

从c++14 开始,会引入一个 is_final 的判断,确认一个 class 是否被定义为 final,通过使用:

std::is_final<A>::value

来判断 class A 是否为 final。

3. 举例

struct A;
struct A final {}; // OK, definition of struct A,// not value-initialization of variable finalstruct 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()
{
}

final 只是一个标识,在使用成员函数和类定义时有特殊的意义。而在其他情况下可以作为一个对象名、函数名、类名使用。

struct Base
{virtual void foo();
};struct A : Base
{void foo() final; // Base::foo is overridden and A::foo is the final overridevoid 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

注意这里的几个注意点:

  • A 类中 foo() 继承自 Base 类,并把自身定义为 final,即后续继承A 的子类无法再重写 foo();
  • A 类中 bar() 使用了final,但 final 是用在 virtual 的成员函数;
  • B 类想重写 foo(),是不允许的,因为在A类中已经声明为 final;
  • B 类定义的时候已经被声明为 final,所以无法再被其他类继承,即B 类不会有子类;

4. 原文摘录

Specifies that a virtual function cannot be overridden in a derived class or that a class cannot be derived from.
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.
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 inside a class definition, final may appear in virt-specifier-seq immediately after the declarator and just before function-body.
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-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
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.

相关文章:

c++11 关键字 final 使用

写在最前。。。 请支持原创~~ 1. 功能 用以指定一个 virtual function 不能被派生类重写&#xff1b;或者指定一个 class 不能被继承&#xff1b;2. 语法 对于类中成员函数有两种情况&#xff1a; 只声明时&#xff0c;final 紧跟参数的右括号&#xff0c;如果是纯虚函数&a…...

力扣(LeetCode)426. 将二叉搜索树转化为排序的双向链表(2023.02.28)

将一个 二叉搜索树 就地转化为一个 已排序的双向循环链表 。 对于双向循环列表&#xff0c;你可以将左右孩子指针作为双向循环链表的前驱和后继指针&#xff0c;第一个节点的前驱是最后一个节点&#xff0c;最后一个节点的后继是第一个节点。 特别地&#xff0c;我们希望可以…...

华为OD机试真题Python实现【玩牌高手】真题+解题思路+代码(20222023)

玩牌高手 题目 给定一个长度为N的整数数组,表示一个选手在N轮内选择的牌面分数, 选手基于规则选牌,请计算所有轮结束后其可以获得的最高总分数。 选择规则如下: 在每轮里选手可以选择获取该轮牌面,则其总分数加上该轮牌面分数为其新的总分数选手也可不选择本轮牌面,直接…...

“速通“ 老生常谈的HashMap [实现原理源码解读]

&#x1f473;我亲爱的各位大佬们好&#x1f618;&#x1f618;&#x1f618; ♨️本篇文章记录的为 HashMap 实现原理&&源码解读 相关内容&#xff0c;适合在学Java的小白,帮助新手快速上手,也适合复习中&#xff0c;面试中的大佬&#x1f649;&#x1f649;&#x1f…...

Linux系统介绍及熟悉Linux基础操作

一、什么是Liunx Linux&#xff0c;全称GNU/Linux&#xff0c;是一种免费使用和自由传播的类UNIX操作系统&#xff0c;其内核由林纳斯本纳第克特托瓦兹&#xff08;Linus Benedict Torvalds&#xff09;于1991年10月5日首次发布&#xff0c;它主要受到Minix和Unix思想的启发&am…...

mysql数据库limit的四种用法

文章目录前言一、语法二、参数说明三、常用示例-4种用法总结前言 mysql数据库中limit子句可以被用于强制select语句返回指定的记录数。limit接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数&#xff0c;第一个参数指定第一个返回记录行的偏移量&#xff0c…...

嵌入式 linux 系统开发网络的设置

目录 一、前言 二、linux网络静态地址设置 前言 为什么要对linux系统下的ubuntu进行网络设置呢&#xff1f; 因为我们在嵌入式开发中&#xff0c;我们要保证windows系统、linux系统、开发板的ip要处于同一个网段&#xff0c;而默认ubuntu下的linux系统的ip是动态分配的&#…...

算法设计与分析——十大经典排序算法一(1--5)

目录 算法设计与分析——十大经典排序算法 第1关&#xff1a;冒泡排序 参考代码 第2关&#xff1a;选择排序 参考代码 第3关&#xff1a;插入排序 参考代码 第4关&#xff1a;希尔排序 参考代码 第5关&#xff1a;归并排序 参考代码 作者有言 一个不知名大学生&#x…...

六.慕课的冲击:知识何以有力量?

6.1知识就是力量?【单选题】关于技术进步,以下说法错误的是&#xff08; &#xff09;。A、技术进步可以不依靠知识积累B、知识的力量推动技术进步C、技术黑箱换句话说即是天上掉馅饼D、专利保护产生的垄断利润,构成创新动力我的答案&#xff1a;A【判断题】罗伯特索洛认为,技…...

SQL基础

sql基础笔记 DATEDIFF() 函数返回两个日期之间的时间。 DATEDIFF&#xff08;parameter1&#xff0c;parameter2&#xff0c;parameter3&#xff09; parameter1&#xff1a;可为 年月日时分秒或周 parameter2&#xff0c;parameter3&#xff1a;合法的日期 如&#xff1a…...

脏牛复现(CVE2016-5195)

nmap扫描全网段&#xff0c;发现存货主机&#xff0c;ip为192.168.85.141nmap 192.168.85.0/24nmap 扫描端口&#xff0c;发现80端口&#xff0c;访问该网站nmap -p1-65535 192.168.85.141扫描该网站目录&#xff0c;什么也没扫出来 &#xff0c;dirb扫描目录的字典在usr/share…...

Redis源码---内存友好的数据结构该如何细化设计

目录 前言 内存友好的数据结构 SDS 的内存友好设计 redisObject 结构体与位域定义方法 嵌入式字符串 压缩列表和整数集合的设计 节省内存的数据访问 前言 Redis 是内存数据库&#xff0c;所以&#xff0c;高效使用内存对 Redis 的实现来说非常重要而实际上&#xff0c;R…...

获取 本周、本月、本年 的开始或结束时间

获取 本周、本月、本年 的开始或结束时间 public class DateTimeUtil{// 获取 本周、本月、本年 的开始或结束时间/// <summary>/// 获取开始时间/// </summary>/// <param name"TimeType">Week、Month、Year</param>/// <param name&quo…...

算法训练营 day58 动态规划 判断子序列 不同的子序列

算法训练营 day58 动态规划 判断子序列 不同的子序列 判断子序列 392. 判断子序列 - 力扣&#xff08;LeetCode&#xff09; 给定字符串 s 和 t &#xff0c;判断 s 是否为 t 的子序列。 字符串的一个子序列是原始字符串删除一些&#xff08;也可以不删除&#xff09;字符而…...

优思学院|DFMEA是全球制造业的必修课!

DFMEA&#xff08;Design Failure Mode and Effects Analysis&#xff09;是一种分析技术&#xff0c;在产品设计的早期阶段识别和解决潜在的失效问题。它通过分析设计的各个方面&#xff0c;识别潜在的失效模式和影响&#xff0c;并提出相应的改进措施&#xff0c;以减少失效的…...

【Day02数据结构 空间复杂度】

最近太忙了都好久没有更新博客了,太难了,抽空水一篇文章,大佬们多多支持. 上篇:时间复杂度分析 目录 前言 一、空间复杂度概念&#xff1f; 二、实例展示 三、.有复杂度要求的算法题练习 1.题目链接&#xff1a;力扣--消失的数字 2.题目链接&#xff1a;力扣--旋转数组 总结: 1…...

多数据库管理工具哪家强?ChatGPT点评,第一位并不是Navicat

SQL逐渐成为职场必备的编程语言&#xff0c;相信大家都不陌生。SQL是一种结构化查询语言&#xff0c;是用于数据库之间通信的编程语言。每个数据库都有着自己独特的访问规则&#xff0c;但大体上是遵循SQL标准。 因此&#xff0c;辗转于不同的数据库之间&#xff0c;开发者或D…...

UnityShader常用函数(UnityShader内置函数、CG和GLSL内置函数等)

空间变换函数函数名描述float4 UnityWorldToClipPos(float3 pos )把世界坐标空间中某一点pos变换到齐次裁剪空间float4 UnityViewToClipPos(float3 pos )把观察坐标空间中某一点pos变换到齐次裁剪空间float3 UnityObjectToViewPos(float3 pos或float4 pos)模型局部空间坐标系中…...

Springboot自定义注解-1

注解用于修饰其他的注解(纪委&#xff1a;管干部的干部) ① Retention&#xff1a;定义注解的保留策略 Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中&#xff0c;在class字节码文件中不包含 Retention(RetentionPolicy.CLASS) …...

经纬度标定及大地坐标系相关概念(一)

经纬度标定及大地坐标系相关概念&#xff08;一&#xff09;一、背景二、经纬度的概念三、大地坐标系四、大地坐标系的分类五、各类坐标系介绍5.1 我国地理坐标系5.1.1 北京54坐标系5.1.2 1980西安坐标系5.1.3 2000国家大地坐标系5.2 世界大地坐标系5.1.1 WGS84坐标系5.3 加密坐…...

【HarmonyOS 5.0】DevEco Testing:鸿蒙应用质量保障的终极武器

——全方位测试解决方案与代码实战 一、工具定位与核心能力 DevEco Testing是HarmonyOS官方推出的​​一体化测试平台​​&#xff0c;覆盖应用全生命周期测试需求&#xff0c;主要提供五大核心能力&#xff1a; ​​测试类型​​​​检测目标​​​​关键指标​​功能体验基…...

Day131 | 灵神 | 回溯算法 | 子集型 子集

Day131 | 灵神 | 回溯算法 | 子集型 子集 78.子集 78. 子集 - 力扣&#xff08;LeetCode&#xff09; 思路&#xff1a; 笔者写过很多次这道题了&#xff0c;不想写题解了&#xff0c;大家看灵神讲解吧 回溯算法套路①子集型回溯【基础算法精讲 14】_哔哩哔哩_bilibili 完…...

蓝桥杯 2024 15届国赛 A组 儿童节快乐

P10576 [蓝桥杯 2024 国 A] 儿童节快乐 题目描述 五彩斑斓的气球在蓝天下悠然飘荡&#xff0c;轻快的音乐在耳边持续回荡&#xff0c;小朋友们手牵着手一同畅快欢笑。在这样一片安乐祥和的氛围下&#xff0c;六一来了。 今天是六一儿童节&#xff0c;小蓝老师为了让大家在节…...

《用户共鸣指数(E)驱动品牌大模型种草:如何抢占大模型搜索结果情感高地》

在注意力分散、内容高度同质化的时代&#xff0c;情感连接已成为品牌破圈的关键通道。我们在服务大量品牌客户的过程中发现&#xff0c;消费者对内容的“有感”程度&#xff0c;正日益成为影响品牌传播效率与转化率的核心变量。在生成式AI驱动的内容生成与推荐环境中&#xff0…...

UR 协作机器人「三剑客」:精密轻量担当(UR7e)、全能协作主力(UR12e)、重型任务专家(UR15)

UR协作机器人正以其卓越性能在现代制造业自动化中扮演重要角色。UR7e、UR12e和UR15通过创新技术和精准设计满足了不同行业的多样化需求。其中&#xff0c;UR15以其速度、精度及人工智能准备能力成为自动化领域的重要突破。UR7e和UR12e则在负载规格和市场定位上不断优化&#xf…...

MySQL用户和授权

开放MySQL白名单 可以通过iptables-save命令确认对应客户端ip是否可以访问MySQL服务&#xff1a; test: # iptables-save | grep 3306 -A mp_srv_whitelist -s 172.16.14.102/32 -p tcp -m tcp --dport 3306 -j ACCEPT -A mp_srv_whitelist -s 172.16.4.16/32 -p tcp -m tcp -…...

回溯算法学习

一、电话号码的字母组合 import java.util.ArrayList; import java.util.List;import javax.management.loading.PrivateClassLoader;public class letterCombinations {private static final String[] KEYPAD {"", //0"", //1"abc", //2"…...

SQL Server 触发器调用存储过程实现发送 HTTP 请求

文章目录 需求分析解决第 1 步:前置条件,启用 OLE 自动化方式 1:使用 SQL 实现启用 OLE 自动化方式 2:Sql Server 2005启动OLE自动化方式 3:Sql Server 2008启动OLE自动化第 2 步:创建存储过程第 3 步:创建触发器扩展 - 如何调试?第 1 步:登录 SQL Server 2008第 2 步…...

Spring Boot SQL数据库功能详解

Spring Boot自动配置与数据源管理 数据源自动配置机制 当在Spring Boot项目中添加数据库驱动依赖&#xff08;如org.postgresql:postgresql&#xff09;后&#xff0c;应用启动时自动配置系统会尝试创建DataSource实现。开发者只需提供基础连接信息&#xff1a; 数据库URL格…...

黑马Javaweb Request和Response

一.介绍 在 Web 开发中&#xff0c;HttpServletRequest 和 HttpServletResponse 是两个非常重要的类&#xff0c;它们分别用于处理客户端的请求和服务器的响应。以下是它们的详细说明和使用方法&#xff1a; 1. HttpServletRequest HttpServletRequest 是一个接口&#xff0…...