C++11中override的使用

C++11中override的使用override 是 C 11 中的一个继承控制关键字 override 确保在派生类中声明的重载函数跟基类的虚函数有相同的声明 override 明确地表示一个函数是对基类中一个虚函数的重载 更重要的是 它会检查基类虚函数和派生类中重载函数的签名不匹配问题 如果签名不匹配 编译器会发出错误信息 override 表示函数应当重写基类中的虚函数 用于派生类的虚函数中 override Spec

override是C++11中的一个继承控制关键字。override确保在派生类中声明的重载函数跟基类的虚函数有相同的声明

override明确地表示一个函数是对基类中一个虚函数的重载。更重要的是,它会检查基类虚函数和派生类中重载函数的签名不匹配问题。如果签名不匹配,编译器会发出错误信息。

override表示函数应当重写基类中的虚函数(用于派生类的虚函数中)

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

The override special identifier means that the compiler will check the base class(es) to see if there is a virtual function with this exact signature. And if there is not,the compiler will indicate an error.

Declaring a method as “override” means that that method is intended to rewrite a(virtual) method on the base class. The overriding method must have same signature (at least for the input parameters) as the method it intends to rewrite.

Adding”override” clearly disambiguates this: through this, one is telling the compiler that three things are expecting:

(1)、there is a method with the same name in the superclass.

(2)、this method in the superclass is declared as “virtual” (that means, intended to be rewritten).

(3)、the method in the superclass has the same (input*) signature as the method in the subclass(the rewriting method).

If any of these is false, then an error is signaled.

The override keyword serves two purposes:

(1)、It shows the reader of the code that “this is a virtual method, that is overriding a virtual method of the base class.”

(2)、The compiler also knows that it’s an override, so it can “check” that you are not altering/adding new methods that you think are overrides.。

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

#include "override.hpp" #include 
  
    // // reference: http://stackoverflow.com/questions//what-is-the-override-keyword-in-c-used-for struct base_override { virtual void foo() = 0; }; struct derived_override : base_override { virtual void foo() override { std::cout << "__PRETTY_FUNCTION__" << std::endl; } }; int test_override1() { base_override* override = new derived_override(); override->foo(); return 0; } // // reference: http://en.cppreference.com/w/cpp/language/override struct A { virtual void foo(); void bar(); }; 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 }; // // reference: https://msdn.microsoft.com/en-us/library/jj.aspx class BaseClass { virtual void funcA(); virtual void funcB() const; virtual void funcC(int = 0); void funcD(); }; class DerivedClass : public BaseClass { virtual void funcA() override; // ok // virtual void funcB() override; // compiler error: DerivedClass::funcB() does not // override BaseClass::funcB() const // virtual void funcC(double = 0.0) override; // compiler error: // DerivedClass::funcC(double) does not // override BaseClass::funcC(int) // void funcD() override; // compiler error: DerivedClass::funcD() does not // override the non-virtual BaseClass::funcD() }; // // reference: https://segmentfault.com/a/98366 struct B_ { virtual void f(); virtual void g() const; virtual void h(char); void k(); // non-virtual virtual void m() final; }; struct D_ : B_ { void f() override; // OK: 重写 B::f() // void g() override; // error: 不同的函数声明,不能重写 virtual void h(char); // 重写 B::h( char ); 可能会有警告 // void k() override; // error: B::k() 不是虚函数 // virtual void m(); // error: m()在基类中声明禁止重写 }; 
  

GitHub:https://github.com/fengbingchun/Messy_Test

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/223668.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午1:40
下一篇 2026年3月17日 下午1:41


相关推荐

  • jq tmpl输出编码html,jQuery tmpl 讲解「建议收藏」

    jq tmpl输出编码html,jQuery tmpl 讲解「建议收藏」2016-07-0114:30陈铭竑1、什么是jQuery-tmpl(1)jQuery的一个类库(2)一个轻量级的前端模板引擎(vue.js也是一种前端模板引擎)(3)可以在模板中实现逻辑运算2、jQuery-tmpl的语法(1)占位:${变量}或{{=变量}}注:=和变量之间一定要有空格(2)循环{{each(i,obj)objs}}…{{/each}}(3)选择{{if条件}}….

    2022年6月16日
    45
  • 预写式日志(Write-Ahead Logging (WAL))

    预写式日志(Write-Ahead Logging (WAL))

    2021年11月25日
    44
  • 文件操作(File类等)API摘要[通俗易懂]

    文件操作(File类等)API摘要[通俗易懂]Console此类包含多个方法,可访问与当前Java虚拟机关联的基于字符的控制台设备(如果有)。虚拟机是否具有控制台取决于底层平台,还取决于调用虚拟机的方式。如果虚拟机从一个交互式命令行开始启动,且没有重定向标准输入和输出流,那么其控制台将存在,并且通常连接到键盘并从虚拟机启动的地方显示。如果虚拟机是自动启动的(例如,由后台作业调度程序启动),那么它通常没有控制台。如果此虚拟机具

    2022年5月27日
    38
  • App 抓包-Fiddler简单使用教程

    App 抓包-Fiddler简单使用教程App抓包-Fiddler简单使用教程环境说明Windows10家庭版小米10MUUI12.5.3稳定版FiddlerEverywhere2.1.1注意Fiddler在进行品牌升级后,推出了多款同类产品,虽然基础功能都相同,但各有偏重,在使用上也存在一些差异。比如博主早期使用过的Fiddler绿色版(对应现在的FiddlerClassic)在操作上和本博客介绍的FiddlerEverywhere几乎完全不同。目标使用Fiddle

    2022年5月7日
    79
  • 因果图-判定表法

    因果图-判定表法一、应用场合界面中有多个控件,控件之间存在组合和限制关系,不同输入条件组合会对应不同的输出结果,为了理清每种输入条件组合和输出结果之间的对应关系,可以使用因果图/判定表法。注意:因果图/判定表法适合测试组合数量较少的情况,如果组合数量较多时,适合使用正交排列法。(更高效)二、因果图法基础1、因果图法因:输入条件果:输出结果因果图法:用画图的方式表示输入条件(因)和输出结果(果)之间的关系。2、图形符号(了解)…

    2022年8月14日
    4
  • Java多态原理

    Java多态原理Java 多态原理最近在准备面试 顺便复习以下 Java 最基础的东西仅作参考 Java 多态原理 Java 多态原理 0 什么是多态 1 jvm 内部类信息 2 多态的实现原理为了更好地理解多态的原理 首先必须对 jvm 内存模型 Java 方法调用等知识有点了解 0 什么是多态多态分为两种 本文着重讲解运行时多态 编译时多态 也叫做静态多态 指的是方法的重载 在同一个类中 同样的方法签名却有不同的参数 编译时通过静态绑定就能实现 运行时多态 也叫做动态多态 指的是方法的重写 在具有继承关系的类中 子类重

    2026年3月16日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号