C++ 'override' 关键字只是检查被覆盖的虚方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13880205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:47:18  来源:igfitidea点击:

Is the 'override' keyword just a check for a overridden virtual method?

c++c++11inheritanceoverridingvirtual-functions

提问by aiao

As far as I understand, the introduction of overridekeyword in C++11 is nothing more than a check to make sure that the function being implemented is the overrideing of a virtualfunction in the base class.

据我了解,overrideC++11中关键字的引入无非是为了确保所实现overridevirtual函数是基类中某个函数的ing 。

Is that it?

是这样吗?

回答by Kerrek SB

That's indeed the idea. The point is that you are explicit about what you mean, so that an otherwise silent error can be diagnosed:

确实是这个想法。关键是你明确表达了你的意思,这样就可以诊断出一个无声的错误:

struct Base
{
    virtual int foo() const;
};

struct Derived : Base
{
    virtual int foo()   // whoops!
    {
       // ...
    }
};

The above code compiles, but is not what you may have meant (note the missing const). If you said instead, virtual int foo() override, then you would get a compiler error that your function is not in fact overriding anything.

上面的代码可以编译,但不是您的意思(注意缺少的const)。如果你改为,virtual int foo() override,那么你会得到一个编译器错误,你的函数实际上没有覆盖任何东西。

回答by user1284631

Wikipedia quote:

维基百科引用:

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 error out.

覆盖特殊标识符意味着编译器将检查基类以查看是否存在具有此确切签名的虚函数。如果没有,编译器就会出错。

http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final

http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final

Edit (attempting to improve a bit the answer):

编辑(试图改进一点答案):

Declaring a method as "override" means that that method is intendedto 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.

将方法声明为“覆盖”意味着该方法旨在重写基类上的(虚拟)方法。覆盖方法必须与它打算重写的方法具有相同的签名(至少对于输入参数)。

Why is this necessary? Well, the following two common error cases are prevented:

为什么这是必要的?好吧,防止了以下两种常见的错误情况:

  1. one mistypes a type in the new method. The compiler, unaware that it is intending to write a previous method, simply adds it to the class as a new method. The problem is that the old method is still there, the new one is added just as an overload. In this case, all calls towards the old method will function just as before, without any change in behavior (which would have been the very purpose of the rewriting).

  2. one forgets to declare the method in the superclass as "virtual", but still attempts to re-write it in a subclass. While this will be apparently accepted, the behavior won't be exactly as intended: the method is not virtual, so access through pointers towards the superclass will end calling the old (superclass') method instead of the new (subclass') method.

  1. 一个人在新方法中错误输入了一个类型。编译器不知道它打算编写以前的方法,只是将它作为新方法添加到类中。问题是旧方法仍然存在,新方法只是作为重载添加的。在这种情况下,对旧方法的所有调用都将像以前一样运行,行为没有任何改变(这正是重写的目的)。

  2. 忘记将超类中的方法声明为“虚拟”,但仍尝试在子类中重写它。虽然这显然会被接受,但行为不会完全符合预期:该方法不是虚拟的,因此通过指向超类的指针访问将结束调用旧的(超类)方法而不是新的(子类)方法。

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)
  1. 超类中有一个同名的方法
  2. 超类中的这个方法被声明为“虚拟”(这意味着,打算重写)
  3. 超类中的方法与子类中的方法(重写方法)具有相同的(输入*)签名

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

如果其中任何一个为假,则发出错误信号。

* note: the output parameter is sometimes of different, but related type. Read about covariant and contravariant transformations if interested.

* 注意:输出参数有时不同,但类型相关。如果有兴趣,请阅读协变和逆变变换。

回答by user3792211

Found "override" is useful when somebody updated base class virtual method signature such as adding an optional parameter but forgot to update derived class method signature. In that case the methods between the base and the derived class are no longer polymorphic relation. Without the override declaration, it is hard to find out this kind of bug.

当有人更新了基类虚拟方法签名(例如添加可选参数)但忘记更新派生类方法签名时,发现“覆盖”很有用。在这种情况下,基类和派生类之间的方法不再是多态关系。没有覆盖声明,很难发现这种错误。

回答by RonaldBarzell

Yes, this is so. It's a check to make sure one doesn't try an override and mess it up through a botched signature. Here's a Wiki page that explains this in detail and has a short illustrative example:

是的,就是这样。这是一种检查,以确保人们不会尝试覆盖并通过拙劣的签名弄乱它。这是一个 Wiki 页面,详细解释了这一点,并有一个简短的说明性示例:

http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final

http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final