“override”关键字只是一个覆盖虚拟方法的检查? [英] Is the 'override' keyword just a check for a overriden virtual method?

查看:122
本文介绍了“override”关键字只是一个覆盖虚拟方法的检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解,在C ++ 11中引入 override 关键字只是一个检查,以确保正在实现的功能是覆盖基本类中的虚拟函数。

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

是这样吗?

推荐答案

这的确是这个想法。关键是你明确了你的意思,所以可以诊断一个否则无声的错误:

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!
    {
       // ...
    }
};

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

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.

这篇关于“override”关键字只是一个覆盖虚拟方法的检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆