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

c++11 关键字 override 使用

写在最前。。。

请支持原创~~ 

1. 功能

用在类中成员函数声明的地方,用以标记一个virtual function 是重写另一个 virtual function;

 

2. 语法

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

3. 举例

 

#include <iostream>struct A
{virtual void foo();void bar();virtual ~A();
};// member functions definitions of struct A:
void A::foo() { std::cout << "A::foo();\n"; }
A::~A() { std::cout << "A::~A();\n"; }struct B : A
{
//  void foo() const override; // Error: B::foo does not override A::foo// (signature mismatch)void foo() override; // OK: B::foo overrides A::foo
//  void bar() override; // Error: A::bar is not virtual~B() override; // OK: `override` can also be applied to virtual// special member functions, e.g. destructorsvoid override(); // OK, member function name, not a reserved keyword
};// member functions definitions of struct B:
void B::foo() { std::cout << "B::foo();\n"; }
B::~B() { std::cout << "B::~B();\n"; }
void B::override() { std::cout << "B::override();\n"; }int main()
{B b;b.foo();b.override(); // OK, invokes the member function `override()`int override{42}; // OK, defines an integer variablestd::cout << "override: " << override << '\n';
}

重写的函数加上override,那么该成员函数需要满足:

  • 成员函数为 虚函数;
  • 成员函数从父类继承,在子类重写;

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

 

结果:

B::foo();
B::override();
override: 42
B::~B();
A::~A();

 

4. 原文摘录 

Specifies that a virtual function overrides another virtual function.
The identifier override, if used, appears immediately after the declarator in the syntax of a member function declaration or a member function definition inside a class definition.


1) In a member function declaration, override 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, override may appear in virt-specifier-seq immediately after the declarator and just before function-body.
In both cases, virt-specifier-seq, if used, is either override or final, or final override or override final.


In a member function declaration or definition, override specifier ensures that the function is virtual and is overriding a virtual function from a base class. The program is ill-formed (a compile-time error is generated) if this is not true.


override is an identifier with a special meaning when used after member function declarators: it's not a reserved keyword otherwise.

 

 

http://www.lryc.cn/news/25678.html

相关文章:

  • 从16K跳槽到20K,最后算下来年薪却还降了,我笑了····
  • 线性表 链表表示
  • 面试题JavaScript篇(二)
  • 项目管理工具dhtmlxGantt甘特图入门教程(十五):从MS项目导入/导出(下)
  • 2023 年 6 大智能合约语言
  • 家用洗地机哪款最好用?全球洗地机十大品牌
  • 【2223sW2】LOG1
  • Spring Cloud配置application.yml与bootstrap.yml区别及多profile配置 | Spring Cloud 6
  • springboot通过aop实现全局日志(是否自定义注解都可以)
  • k8s面试题-进阶
  • 预览版Edge申请微软new Bing失败解决方案
  • Spring中Bean生命周期及循环依赖
  • 【3.1】MySQL锁、动态规划、Redis缓存,过期删除与淘汰策略
  • Python+Yolov5跌倒检测 摔倒检测 人物目标行为 人体特征识别
  • 计算机底层:储存器的性能指标(CPU和内存等硬件的性能以及 对比标准)
  • 操作留痕功能实现与探讨
  • 深入浅出消息队列MSMQ
  • Maven多模块开发
  • QT之OpenGL帧缓冲
  • $ 6 :选择、循环
  • 【项目设计】高并发内存池 (四)[pagecache实现]
  • 玩转qsort——“C”
  • 【干货】又是一年跳槽季!Nginx 10道核心面试题及解析
  • 【线程安全的HashMap有哪些,CurrentHashMap底层是怎么实现线程安全的】
  • C语言-结构体【详解】
  • 浏览器输入url到页面渲染完成经历了哪些步骤
  • 大数据技术之Hadoop(Yarn)
  • 5.建造者模式
  • 数据库基础-数据库基本概念(1-1)
  • 学习笔记-架构的演进之服务容错策略-服务发现-3月day01