虚拟/纯虚拟解释 [英] Virtual/pure virtual explained

查看:87
本文介绍了虚拟/纯虚拟解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果函数定义为虚函数,并且与纯虚函数相同,那到底是什么意思?

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


在面向对象的编程中,在诸如C ++和Object Pascal之类的语言中,虚函数或虚方法是可继承且可重写的函数或方法,有助于进行动态调度。这个概念是面向对象编程(OOP)的(运行时)多态性部分的重要组成部分。简而言之,虚函数定义了要执行的目标函数,但在编译时可能不知道该目标。

In object-oriented programming, in languages such as C++, and Object Pascal, a virtual function or virtual method is an inheritable and overridable function or method for which dynamic dispatch is facilitated. This concept is an important part of the (runtime) polymorphism portion of object-oriented programming (OOP). In short, a virtual function defines a target function to be executed, but the target might not be known at compile time.

非虚拟函数,当覆盖虚拟函数时,派生最广的版本将用于类层次结构的所有级别,而不仅仅是创建它的级别。因此,如果基类的一个方法调用一个虚拟方法,则将使用派生类中定义的版本而不是基类中定义的版本。

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.

而。.


纯虚函数或纯虚方法是如果派生类不是抽象的,则必须由派生类实现的虚函数。

A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is not abstract.

当存在纯虚方法时,该类为抽象类,不能单独实例化。而是必须使用实现纯虚拟方法的派生类。在基类中根本没有定义纯虚拟的,因此派生类必须对其进行定义,或者该派生类也是抽象的,无法实例化。只能实例化没有抽象方法的类。

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.

这篇关于虚拟/纯虚拟解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆