问题描述
如果函数定义为虚函数,并且与纯虚函数相同,那到底是什么意思?
What exactly does it mean if a function is defined as virtual and is that the same as pure virtual?
推荐答案
从
...
From Wikipedia's Virtual function...
非虚拟函数,当覆盖虚拟函数时,派生最广的版本将用于类层次结构的所有级别,而不仅仅是创建它的级别。因此,如果基类的一个方法调用一个虚拟方法,则将使用派生类中定义的版本而不是基类中定义的版本。
Unlike a non-virtual function, when a virtual function is overridden the most-derived version is used at all levels of the class hierarchy, rather than just the level at which it was created. Therefore if one method of the base class calls a virtual method, the version defined in the derived class will be used instead of the version defined in the base class.
这与非虚拟函数相反,非虚拟函数仍可以在派生类中重写,但新版本仅由派生类使用,并且
This is in contrast to non-virtual functions, which can still be overridden in a derived class, but the "new" version will only be used by the derived class and below, but will not change the functionality of the base class at all.
而。.
当存在纯虚方法时,该类为抽象类,不能单独实例化。而是必须使用实现纯虚拟方法的派生类。在基类中根本没有定义纯虚拟的,因此派生类必须对其进行定义,或者该派生类也是抽象的,无法实例化。只能实例化没有抽象方法的类。
When a pure virtual method exists, the class is "abstract" and can not be instantiated on its own. Instead, a derived class that implements the pure-virtual method(s) must be used. A pure-virtual isn't defined in the base-class at all, so a derived class must define it, or that derived class is also abstract, and can not be instantiated. Only a class that has no abstract methods can be instantiated.
虚拟提供了一种重写基类功能的方法,而纯虚拟的则需要它。
A virtual provides a way to override the functionality of the base class, and a pure-virtual requires it.
这篇关于虚拟/纯虚拟解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!