纯虚函数声明中的“= 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
= 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.
这意味着虚拟函数是纯,这意味着您不能这样调用它:该函数没有任何代码,因此
= 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.基本上,这意味着该函数没有代码。这意味着您不能使用此类的实例。相反,它只能是基类。
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.