纯虚函数声明中的“= 0”是什么意思?

发布于 2024-09-17 18:14:52 字数 561 浏览 6 评论 0原文

可能的重复:
C++ 虚拟/纯虚拟解释
C++ 中的虚函数实例化之间有什么区别< br> 为什么纯虚函数初始化为0?

这是一个有人给我的一些类声明中的方法。我不知道“..=0”是什么意思。它是什么?

virtual void Print() const = 0;

Possible Duplicates:
C++ Virtual/Pure Virtual Explained
What's the difference between virtual function instantiations in c++
Why pure virtual function is initialized by 0?

This is a method in some class declaration that someone gave me. And I don't know what '..=0' means. What is it?

virtual void Print() const = 0;

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

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

发布评论

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

评论(3

素手挽清风 2024-09-24 18:14:53

= 0 使函数纯虚拟 ,将类呈现为抽象类

抽象类基本上是一种接口,派生类需要实现它才能实例化。然而,还有更多内容,它是 C++ 中面向对象编程的一些基础知识。如果你不知道这些,你需要回到课本上去读。如果不理解它们,你就无法进步。

也就是说,请参阅此相关问题< /strong> 有关什么是虚函数和纯虚函数的一些解释。与往常一样,C++ 常见问题解答 是解答此类问题的绝佳资源。

The = 0 makes the function pure virtual, rendering the class an abstract class.

An abstract class basically is a kind of interface, which derived classes need to implement in order to be instantiable. However, there's much more to this, and it is some of the very basics of object-oriented programming in C++. If you don't know these, you need to go back to the textbook and read up. There's no way you can advance without understanding them.

That said, see this related question for some explanations of what virtual and pure virtual functions are. And as always, the C++ FAQ is an excellent resource for such questions.

瑕疵 2024-09-24 18:14:53

这意味着虚拟函数是,这意味着您不能这样调用它:该函数没有任何代码,因此 = 0。只有通过派生类并重写函数才能调用它。具有纯虚函数的类无法实例化,因此在某些语言中称为抽象类、接口。

It means that the virtual function is pure, meaning that you cannot call it as such: the function doesn't have any code to it, hence the = 0. Only by deriving the class and overriding the function you can call it. The class with pure virtual functions cannot be instantiated so they are called abstract classes, interfaces in some languages.

窝囊感情。 2024-09-24 18:14:53

基本上,这意味着该函数没有代码。这意味着您不能使用此类的实例。相反,它只能是基类。

Basically, it means the function has no code. This means that you cannot use instances of this class. Rather, it can only be a base class.

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