C++以“= 0”结尾的头文件和函数声明

发布于 2024-08-27 01:29:59 字数 201 浏览 12 评论 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 技术交流群。

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

发布评论

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

评论(4

叹沉浮 2024-09-03 01:29:59

这是一个纯虚函数。这意味着子类必须实现此功能,否则它们是抽象的,这意味着您无法创建该类的对象。

class ISomeInterface {
public:
    virtual std::string ToString( ) = 0;
}

class SomeInterfaceImpl : public ISomeInterface {
public:
    virtual std::string ToString( ) {
        return "SomeInterfaceImpl";
    }
}

这个想法是,一个类可以公开某个方法,但子类必须实现它。在此示例中,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.

class ISomeInterface {
public:
    virtual std::string ToString( ) = 0;
}

class SomeInterfaceImpl : public ISomeInterface {
public:
    virtual std::string ToString( ) {
        return "SomeInterfaceImpl";
    }
}

The idea is, that a class can expose a certain method, but subclasses have to implement it. In this example, ISomeInterface exposes a ToString method, but there is no sensible default implementation for that, so it makes the method pure virtual. Subclasses like SomeInterfaceImpl can then provide a fitting implementation.

司马昭之心 2024-09-03 01:29:59

= 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).

温柔少女心 2024-09-03 01:29:59

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

嘦怹 2024-09-03 01:29:59

语法很晦涩,但“=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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文