显式重写虚函数

发布于 2024-10-14 23:25:35 字数 133 浏览 2 评论 0原文

我刚刚发现 C++/CLI 有一个标准 C++ 上不存在的关键字(据我所知):override

我对 C++/CLI 不太了解,所以,有人可以解释一下它的用途是什么,以及它是否是添加到 C++ 中的理想功能?

I just discovered that C++/CLI has a keyword that is not present (AFAIK) on standard C++: override.

I don't know much about C++/CLI, so, can someone explain for which purpose is it included there, and if is it a desirable feature to be added to C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玉环 2024-10-21 23:25:35

override 是一个特殊的关键字扩展Microsoft 可用于 C++/CLI 和 Visual C++ 实现。它类似于 @Override Java 中的 注释或 在 C# 中重写,并提供更好的编译时检查,以防万一您没有重写您想要重写的内容。

从第一个链接:

override 指示托管类型的成员必须重写基类或基接口成员。如果没有要重写的成员,编译器将生成错误。

覆盖在编译本机目标时也有效(不带 /clr)。有关详细信息,请参阅覆盖说明符和本机编译。

override 是上下文相关的关键字。有关详细信息,请参阅上下文相关关键字。

从 C++11 标准开始,override 说明符现在是一个标准化关键字。支持仍然有限,根据 Apache StdCxx 的页面override 受 GCC 4.7+、Intel C++ 12.0+ 和 Visual C++ 2012 支持(在 Visual C++ 2005 中具有预标准化支持)。

override is a special keyword extension from Microsoft that can be used in C++/CLI and Visual C++ implementations. It is similar to the @Override annotation in Java or override in C#, and provides better compile time checks just in case you didn't override something you meant to.

From the first link:

override indicates that a member of a managed type must override a base class or a base interface member. If there is no member to override, the compiler will generate an error.

override is also valid when compiling for native targets (without /clr). See Override Specifiers and Native Compilations for more information.

override is a context-sensitive keyword. See Context-Sensitive Keywords for more information.

As of the C++11 standard, the override specifier is now a standardized keyword. Support is still limited, and as per this page from Apache StdCxx, override is supported by GCC 4.7+, Intel C++ 12.0+, and Visual C++ 2012 (with pre-standardization support in Visual C++ 2005).

眼泪都笑了 2024-10-21 23:25:35

它可以通过两种方式帮助编译器捕获错误:

  1. 如果您在类中使用 override 声明了一个函数,但基类没有该函数,那么编译器可以告诉您并没有推翻你所认为的自己。如果override不可用,那么编译器将无法识别您的错误 - 它只会假设您打算引入一个新函数。

  2. 如果您的后代类中有一个函数(没有 override),然后您在基类中将相同的函数声明为 virtual,编译器可以告诉您您在基类中的更改影响了后代中原始声明的含义。后代要么需要使用override,要么您需要更改其中一个函数的签名。

此功能已添加到 C++0x 中。

It helps the compiler catch your mistakes two ways:

  1. If you declare a function with override in your class, but the base class doesn't have that function, then the compiler can tell you that you're not overriding what you thought you were. If override weren't available, then the compiler wouldn't be able to recognize your error — it would simply assume that you intended to introduce a new function.

  2. If you have a function in your descendant class (without override), and then you declare that same function as virtual in the base class, the compiler can tell you that your change in the base class has affected the meaning of the original declaration in the descendant. The descendant will either need to use override, or you'll need to change the signature of one of the functions.

This feature is being added to C++0x already.

油饼 2024-10-21 23:25:35

来自此处

如果您在中标记属性或方法
具有virtual的父类
关键字,当派生类时
它,你可以忽略该方法或
财产而不实施它。但如果
您决定实施该财产
或方法,您必须表明您
正在提供新版本
属性或方法。提供一个新的
属性或方法的版本是
称为覆盖它。

当覆盖属性或
方法,您必须通过以下方式表明这一点
override 关键字写入其
对。

现在请点击链接查看一些示例。

From here:

If you flag a property or a method in
a parent class with the virtual
keyword, when deriving a class from
it, you can ignore the method or
property and not implement it. But if
you decide to implement the property
or method, you must indicate that you
are providing a new version of the
property or method. Providing a new
version of a property or a method is
referred to as overriding it.

When overriding a property or a
method, you must indicate this by
writing the override keyword to its
right.

Now follow the link to see some examples.

银河中√捞星星 2024-10-21 23:25:35

C++/CLI 中的 override 关键字来自 .Net,而不是 C++ 本身的一部分。由于已经解释了覆盖,因此您需要知道替代方案。如果您不“覆盖”它,您可能希望将其设为“新”。通过将其设置为“新”,您不会覆盖子类中父类的成员,而是创建一个具有相同名称的新成员。仅当您使用基类指针并将其指向派生类对象时,new 和 override 关键字才会有所不同。

因此,如果您使用基类指针并指向派生类对象:

If you call an "override"n member:
      the derived class member is called
if you call the "new"ed member:
      the base class member is called.

The override keyword in C++/CLI comes from .Net and not a part of C++ itself. Since override has already been explained, you need to know the alternative. If you do not "override" it, you may want to make it "new". By making it "new" you are not overriding the parent class' member in the child but creating a new member with the same name. The new and override keywords only differ when you use a base class pointer and point it to a derived class object.

So, if you use a base class pointer and point to a derived class object:

If you call an "override"n member:
      the derived class member is called
if you call the "new"ed member:
      the base class member is called.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文