显式重写虚函数
我刚刚发现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
override
是一个特殊的关键字扩展Microsoft 可用于 C++/CLI 和 Visual C++ 实现。它类似于@Override
Java 中的 注释或在 C# 中重写
,并提供更好的编译时检查,以防万一您没有重写您想要重写的内容。从第一个链接:
从 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 oroverride
in C#, and provides better compile time checks just in case you didn't override something you meant to.From the first link:
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).它可以通过两种方式帮助编译器捕获错误:
如果您在类中使用
override
声明了一个函数,但基类没有该函数,那么编译器可以告诉您并没有推翻你所认为的自己。如果override
不可用,那么编译器将无法识别您的错误 - 它只会假设您打算引入一个新函数。如果您的后代类中有一个函数(没有
override
),然后您在基类中将相同的函数声明为 virtual,编译器可以告诉您您在基类中的更改影响了后代中原始声明的含义。后代要么需要使用override
,要么您需要更改其中一个函数的签名。此功能已添加到 C++0x 中。
It helps the compiler catch your mistakes two ways:
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. Ifoverride
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.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 useoverride
, or you'll need to change the signature of one of the functions.This feature is being added to C++0x already.
来自此处:
现在请点击链接查看一些示例。
From here:
Now follow the link to see some examples.
C++/CLI 中的 override 关键字来自 .Net,而不是 C++ 本身的一部分。由于已经解释了覆盖,因此您需要知道替代方案。如果您不“覆盖”它,您可能希望将其设为“新”。通过将其设置为“新”,您不会覆盖子类中父类的成员,而是创建一个具有相同名称的新成员。仅当您使用基类指针并将其指向派生类对象时,new 和 override 关键字才会有所不同。
因此,如果您使用基类指针并指向派生类对象:
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: