C++ 中的“override”关键字是做什么用的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18198314/
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
What is the 'override' keyword in C++ used for?
提问by SKPS
I am a beginner in C++. I have come across override
keyword used in the header file that I am working on. May I know, what is real use of override
, perhaps with an example would be easy to understand.
我是 C++ 的初学者。我遇到override
了我正在处理的头文件中使用的关键字。请问一下,什么是真正的用途override
,也许举个例子就容易理解了。
回答by Mats Petersson
The override
keyword serves two purposes:
该override
关键字有两个用途:
- It shows the reader of the code that "this is a virtual method, that is overriding a virtual method of the base class."
- 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.
- 它向代码的读者展示了“这是一个虚方法,它覆盖了基类的一个虚方法”。
- 编译器也知道这是一个覆盖,因此它可以“检查”您没有更改/添加您认为是覆盖的新方法。
To explain the latter:
解释后者:
class base
{
public:
virtual int foo(float x) = 0;
};
class derived: public base
{
public:
int foo(float x) override { ... } // OK
}
class derived2: public base
{
public:
int foo(int x) override { ... } // ERROR
};
In derived2
the compiler will issue an error for "changing the type". Without override
, at most the compiler would give a warning for "you are hiding virtual method by same name".
在derived2
编译器中会发出“更改类型”的错误。如果没有override
,编译器最多会给出“您正在隐藏同名虚拟方法”的警告。
回答by zaufi
And as an addendum to all answers, FYI: override
is not a keyword, but a specialkind of identifier! It has meaning only in the context of declaring/defining virtual functions, in other contexts it's just an ordinary identifier. For details read 2.11.2 of The Standard.
作为所有答案的附录,FYI:override
不是关键字,而是一种特殊的标识符!它仅在声明/定义虚函数的上下文中有意义,在其他上下文中它只是一个普通的identifier。有关详细信息,请阅读标准的2.11.2 。
#include <iostream>
struct base
{
virtual void foo() = 0;
};
struct derived : base
{
virtual void foo() override
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
int main()
{
base* override = new derived();
override->foo();
return 0;
}
Output:
输出:
zaufi@gentop /work/tests $ g++ -std=c++11 -o override-test override-test.cc
zaufi@gentop /work/tests $ ./override-test
virtual void derived::foo()
回答by xorguy
override
is a C++11 keyword which means that a method is an "override" from a method from a base class. Consider this example:
override
是 C++11 关键字,这意味着方法是来自基类的方法的“覆盖”。考虑这个例子:
class Foo
{
public:
virtual void func1();
}
class Bar : public Foo
{
public:
void func1() override;
}
If B::func1()
signature doesn't equal A::func1()
signature a compilation error will be generated because B::func1()
does not override A::func1()
, it will define a new method called func1()
instead.
如果B::func1()
签名不等于A::func1()
签名,则会生成编译错误,因为B::func1()
没有覆盖A::func1()
,它将定义一个新方法func1()
来代替调用。
回答by Giwrgos Tsopanoglou
Wikipedia says:
维基百科说:
Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.
在面向对象编程中,方法覆盖是一种语言特性,它允许子类或子类提供已由其超类或父类之一提供的方法的特定实现。
In detail, when you have an object foo that has a void hello() function:
详细地说,当您有一个具有 void hello() 函数的对象 foo 时:
class foo {
virtual void hello(); // Code : printf("Hello!");
}
A child of foo, will also have a hello() function:
foo 的孩子也将有一个 hello() 函数:
class bar : foo {
// no functions in here but yet, you can call
// bar.hello()
}
However, you may want to print "Hello Bar!" when hello() function is being called from a bar object. You can do this using override
但是,您可能想要打印“Hello Bar!” 当从 bar 对象调用 hello() 函数时。您可以使用覆盖来做到这一点
class bar : foo {
virtual void hello() override; // Code : printf("Hello Bar!");
}