gpt4 book ai didi

c++ - 虚拟/纯虚拟解释

转载 作者:bug小助手 更新时间:2023-10-28 01:31:44 28 4
gpt4 key购买 nike

如果一个函数被定义为虚函数并且与纯虚函数相同,这究竟意味着什么?

最佳答案

来自 Wikipedia's Virtual function...

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.

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

这与非虚函数相反,在派生类中仍然可以被覆盖,但"new"版本只会被派生类及以下使用,但不会改 rebase 类的功能完全没有。

而..

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.

当纯虚方法存在时,类是“抽象的”,不能单独实例化。相反,必须使用实现纯虚拟方法的派生类。纯虚拟根本没有在基类中定义,所以派生类必须定义它,或者派生类也是抽象的,不能被实例化。只能实例化没有抽象方法的类。

虚拟提供了一种覆盖基类功能的方法,而纯虚拟需要它。

关于c++ - 虚拟/纯虚拟解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1306778/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com