虚拟吗?覆盖?还是两个?c++(复制)

时间:2022-11-25 16:54:57

This question already has an answer here:

这个问题已经有了答案:

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?

在过去的几周里,有什么东西在困扰着我的大脑。我学过,当你继承虚拟函数时,你必须添加虚拟函数,让编译器知道要搜索正确的函数。后来我还了解到在c++ 11中有一个新的关键字-重写。现在我有点困惑;我是否需要在我的程序中同时使用虚拟和覆盖关键字,或者最好只使用其中之一?

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?

最好的方法是什么?

1 个解决方案

#1


56  

When you override a function you don't technically need to write either virtual or override.

当重写一个函数时,技术上不需要写虚函数或重写函数。

The original base class declaration needs the keyword virtual to mark it as virtual.

原始基类声明需要关键字virtual将其标记为virtual。

In the derived class the function is virtual by way of having the ¹same 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. E.g. that 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.

但是,如果在技术上不需要重写,那么重写可以通过产生编译错误来帮助避免错误。例如,函数类型与基类函数不完全相同。或者对基类的维护改变了函数的类型,例如添加一个默认参数。

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 is virtual in further derived classes.

同样地,派生类中的虚关键字可以通过确保进一步派生类中的函数仍然是虚的来使这种错误更加微妙。

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.

    使用override(仅)用于派生类的重写。这有助于维护。

Example:

例子:

struct Base { virtual void foo() {} };
struct Derived: Base { void foo() override {} };

Notes:
¹ 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.

注:¹c++支持协变生指针和生参考结果。对于协方差,覆盖的类型并不完全相同。它只是有一个兼容的类型。

#1


56  

When you override a function you don't technically need to write either virtual or override.

当重写一个函数时,技术上不需要写虚函数或重写函数。

The original base class declaration needs the keyword virtual to mark it as virtual.

原始基类声明需要关键字virtual将其标记为virtual。

In the derived class the function is virtual by way of having the ¹same 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. E.g. that 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.

但是,如果在技术上不需要重写,那么重写可以通过产生编译错误来帮助避免错误。例如,函数类型与基类函数不完全相同。或者对基类的维护改变了函数的类型,例如添加一个默认参数。

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 is virtual in further derived classes.

同样地,派生类中的虚关键字可以通过确保进一步派生类中的函数仍然是虚的来使这种错误更加微妙。

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.

    使用override(仅)用于派生类的重写。这有助于维护。

Example:

例子:

struct Base { virtual void foo() {} };
struct Derived: Base { void foo() override {} };

Notes:
¹ 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.

注:¹c++支持协变生指针和生参考结果。对于协方差,覆盖的类型并不完全相同。它只是有一个兼容的类型。