虚拟的?覆盖?或两者?C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39932391/
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
virtual? override? or both? C++
提问by Daniel
in the last weeks something is bugging my brain about virtual and override.
I've learned that when you do inheritance with virtual function you have to add virtual
to let the compiler know to search for the right function.
Afterwards I learned also that in c++ 11 there is a new keyword - override. Now I'm a little confused; Do i need to use both virtual and override keywords in my program, or it's better to use only one of them?
在过去的几周里,关于虚拟和覆盖的事情正在困扰着我的大脑。我了解到,当您使用虚函数进行继承时,您必须添加virtual
以让编译器知道搜索正确的函数。后来我还了解到,在 C++ 11 中有一个新关键字——override。现在我有点困惑;我需要在我的程序中同时使用 virtual 和 override 关键字,还是只使用其中一个更好?
To explain myself - code examples of what i mean:
解释我自己 - 我的意思的代码示例:
class Base
{
public:
virtual void print() const = 0;
virtual void printthat() const = 0;
virtual void printit() const = 0;
};
class inhert : public Base
{
public:
// only virtual keyword for overriding.
virtual void print() const {}
// only override keyword for overriding.
void printthat() const override {}
// using both virtual and override keywords for overriding.
virtual void printit() const override {}
};
What is the best method?
最好的方法是什么?
回答by Cheers and hth. - Alf
When you override a function you don't technically need to write either virtual
or override
.
当您覆盖一个函数时,从技术上讲,您不需要编写virtual
或override
。
The original base class declaration needs the keyword virtual
to mark it as virtual.
原始基类声明需要关键字virtual
将其标记为虚拟。
In the derived class the function is virtual by way of having the 1same type as the base class function.
在派生类中,函数是虚拟的,因为它具有与基类函数相同的类型。
However, an override
can help avoid bugs by producing a compilation error when the intended override isn't technically an override. For instance, the function type isn't exactly like the base class function. Or that a maintenance of the base class changes that function's type, e.g. adding a defaulted argument.
但是,override
当预期的覆盖在技术上不是覆盖时,可以通过产生编译错误来帮助避免错误。例如,函数类型并不完全像基类函数。或者基类的维护会更改该函数的类型,例如添加默认参数。
In the same way, a virtual
keyword in the derived class can make such a bug more subtle by ensuring that the function is still virtual in the further derived classes.
同样,virtual
派生类中的关键字可以通过确保函数在进一步派生类中仍然是虚拟的,从而使这种错误更加微妙。
So the general advice is,
所以一般的建议是,
Use
virtual
for the base class function declaration.
This is technically necessary.Use
override
(only) for a derived class' override.
This helps maintenance.
使用
virtual
基类的函数声明。
这在技术上是必要的。使用
override
(仅)用于派生类的覆盖。
这有助于维护。
Example:
例子:
struct Base { virtual void foo() {} };
struct Derived: Base { void foo() override {} };
Notes:
1 C++ supports covariant raw pointer and raw reference results. With covariance the type of the override isn't exactly the same. It just has a compatible type.
注意:
1 C++ 支持协变原始指针和原始引用结果。对于协方差,覆盖的类型并不完全相同。它只有一个兼容的类型。