C++以“= 0”结尾的头文件和函数声明
我的 .h 文件中有以下代码,但我不确定赋值语句的作用以及如何正确调用它?
virtual void yield() = 0;
我认为该函数默认返回值 0,但由于该函数返回 void 我有点困惑。任何人都可以对此发表评论,也许可以说我如何引用这个作业,我的意思是它在 C++ 术语中是如何调用的?
谢谢。
I have the following code inside the .h file and I'm not sure what does the assignment statement do and how is it called properly?
virtual void yield() = 0;
I thought that the function returns a value of 0 by default but since this function returns void I am a little bit confused. Can anyone comment on this and maybe say how can I refer to this assignment, I mean how is it called in C++ jargon?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个纯虚函数。这意味着子类必须实现此功能,否则它们是抽象的,这意味着您无法创建该类的对象。
这个想法是,一个类可以公开某个方法,但子类必须实现它。在此示例中,
ISomeInterface
公开了一个ToString
方法,但没有合理的默认实现,因此它使该方法成为纯虚拟方法。像SomeInterfaceImpl
这样的子类可以提供合适的实现。This is a pure virtual function. This means, that subclasses have to implement this function, otherwise they are abstract, meaning you cannot create objects of that class.
The idea is, that a class can expose a certain method, but subclasses have to implement it. In this example,
ISomeInterface
exposes aToString
method, but there is no sensible default implementation for that, so it makes the method pure virtual. Subclasses likeSomeInterfaceImpl
can then provide a fitting implementation.= 0
语法声明一个纯虚函数,与返回值无关。如果一个类至少包含一个纯虚函数,那么该类就成为“抽象”的,这意味着它不能被实例化。
在实践中,此类类需要通过子类化和实现虚函数来具体化。
The
= 0
syntax declares a pure virtual function, it has nothing to do with the return value.If a class contains at least one pure virtual function, that makes the class "abstract", which means it cannot be instantiated.
In practice, such classes need to be concretized by subclassing and implementing the virtual function(s).
if 是纯虚拟方法(又名抽象),请查看此处或谷歌 http://www.artima。 com/cppsource/pure_virtual.html
= 0 并不意味着默认返回值,它是通知函数是纯虚函数
if is a pure virtual method (aka abstract) look here or google http://www.artima.com/cppsource/pure_virtual.html
= 0 doesn't mean default return value, it is notification that function is pure virtual
语法很晦涩,但“=0”表示该方法是纯虚函数。它使类变得抽象(你无法实例化它),并且它的实现留给派生类。
当您只想定义一个接口时,可以使用此方法。当您想要定义接口并提供默认实现时,请使用 virtual 关键字。
The syntax is obscure, but the "=0" signifies the method is a pure virtual function. It makes the class abstract (you can't instantiate it) and it's implementation is left to derived classes.
This is used when all you want to define is an interface. Use the virtual keyword when you want to define an interface and also provide a default implementation.