纯虚拟和虚拟有什么区别
我正在修改祖父类是纯虚拟的代码,其中包括函数 XYZ 的纯虚拟版本;然后父类将 XYS 声明为虚拟的,并且它有一个实现。然后子类将 XYZ 声明为常规函数,其实现与父类 1 9 的实现不同,这本身就让我感到困惑)。当我从另一个对象调用函数 XYZ 时,会执行哪个实现?父母一还是孩子一?谢谢
I am modifying code that the grand-parent class is a pure virtual which include a pure virtual version of function XYZ ; the then parent class declares XYS as virtual and it has an implementaion for it. Then the child class declares XYZ as a regular function with a different implementation from that of the parent1 9which in itself is confusing to me). When I call the function XYZ from another object, which implementation gets executed? the parent one or the child one? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
让我们看看:
Let's find out:
纯虚函数不需要定义(与虚函数相反),并且它们使类抽象。抽象类不能创建对象,除非它们充当基类对象。
您的困惑似乎集中在
virtual
关键字的存在或不存在上。如果一个函数在基类中被声明为virtual
,那么派生类的函数,无论是否添加virtual
关键字,都会自动变为virtual< /code> 如果它具有相同的名称、参数类型和常量。
因此,如果您在实际指向
child
对象的grandparent*
或parent*
上调用 XYZ,则child 的 XYZ
对象将被执行。Pure virtual functions don't need a definition (as opposed to virtual functions), and they make their class abstract. Abstract classes can't have an object created from, except when they act as base class object.
Your confusion seems to center around the presence or absence of the
virtual
keyword. If a function is declaredvirtual
in a base class, then a function of a derived class, whether or not you put avirtual
keyword, will automatically becomevirtual
if it has the same name, parameter types and constness.So if you call XYZ on a
grandparent*
orparent*
that actually points to achild
object, then the XYZ of thechild
object will be executed.纯虚函数只是意味着没有纯虚函数的类可以被实例化。一旦父类重写了它,它就变成了一个普通的虚函数。一旦函数在基类中被声明为虚函数,它就始终是虚函数,无论继承类是否将其定义为虚函数。
编辑:这意味着调用它只是像其他任何函数一样调用虚拟函数 - 也就是说,将调用最派生类的实现。
A pure virtual function just means that no class with a pure virtual function can be instantiated. Once the parent class overrides it, it just becomes a normal virtual function. Once a function has been declared virtual in a base class, it is always virtual, regardless of whether or not the inheriting class defines it as virtual.
Edit: That means that calling it is just calling a virtual function like any other- that is, the most derived class's implementation will be called.
纯虚函数通常没有实现,并创建了类的“抽象性”。
编译器不会让您创建抽象类的实例。从此类派生的任何类都保持抽象,除非它继承的所有纯虚函数都已实现(并且不添加任何新函数)。这样的类称为具体类。
请注意,纯虚函数可能会被赋予一个实现(尽管由于语法原因无法内联)。另外,你可以有一个纯虚拟析构函数,然后必须给它一个实现(即使它是一个空的)。
您可以通过在末尾添加 =0 来指示纯虚函数:
A pure virtual function usually has no implementation and creates the "abstractness" of the class.
The compiler will not let you create an instance of a class that is abstract. Any class that derives from this class remains abstract unless all the pure virtual functions it inherits have been implemented (and it doesn't add any new ones). Such a class is called concrete.
Note that a pure virtual function may be given an implementation (although it cannot be inlined for syntax reasons). In addition, you can have a pure virtual destructor, and then it must be given an implementation (even if it is an empty one).
You indicate a pure virtual function by adding =0 at the end thus:
纯虚函数是通过分配
0
来声明的,如上所述。提供纯虚函数的定义是可选,而提供虚函数的定义是强制,否则不会编译。此外,您无法创建定义单个纯虚函数的类实例。Pure virtual function is declared by assigning
0
, as done above. Providing definition for pure virtual functions is optional, while providing definition for virtual functions is compulsory, otherwise it will not compile. Also, you cannot create instance of a class that defines even a single pure virtual function.虚函数是可以在派生类中重写的函数。
纯虚函数是根本没有实现的函数,因此必须在派生类中被重写。
除非您使用自己的实现覆盖任何纯虚函数,否则无法创建具有纯虚函数的类的实例或派生自该类的类的实例。
A virtual function is one that can be overridden in a derived class.
A pure virtual function is one that has no implementation at all and therefore MUST be overridden in a derived class.
You cannot create an instance of a class with a pure virtual function, or a class that derives from it unless you override any pure virtual functions with your own implementation.
对于虚函数,如果您在子类的对象上调用 xyz,始终 xyz 的子版本 (
Child::xyz()
) 将是被执行。非虚拟重写方法不会出现这种情况。然后,如果您有一个指针
Parent* ptr = &child
,则ptr>xyz()
实际上会执行Parent::xyz()
。但就您而言,它是Child::xyz()
。这就是您使用virtual
关键字的原因。In the case of a virtual function, if you call xyz on an object of a child class, always the child version of xyz (
Child::xyz()
) will be executed.It would not be a case with non-virtual overridden methods. Then if you had a pointer
Parent* ptr = &child
,ptr>xyz()
would actually executeParent::xyz()
. But in your case it isChild::xyz()
. That is why you use thevirtual
keyword.