虚函数==函数指针?

发布于 2024-09-02 13:37:39 字数 121 浏览 6 评论 0原文

一组函数指针分组 数据结构中经常会出现 称为虚函数 表(VFT)。

上面的说法让我感觉虚函数==函数指针,是这样吗?

A set of function pointers grouped
into a data structure are often
referred to as a virtual function
table (VFT).

The above statement makes me feel that virtual function == function pointer,is that so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

蘸点软妹酱 2024-09-09 13:37:39

C 中没有对虚函数的内置支持。

在 C++ 中,虚函数是通过 v-table 指定的。 vtable 中的条目可以实现为函数指针。

There is no built-in support for virtual functions in C.

In C++ virtual functions are specified via a v-table. And the entries in a vtable can be implemented as function pointers.

陌生 2024-09-09 13:37:39

这是错误的,因为它们是不同的抽象级别。

一个类比可能会有所帮助:说虚拟函数和函数指针是相同的,就像说轮子和自行车是相同的一样。

虽然函数指针和虚函数在“底层”看起来确实很相似,但它们在概念上是不同的东西(虚函数是类的可重写成员方法,而函数指针只是函数的间接引用) )和语法上(调用它们是完全不同的)。

然而,它们可能具有相同的目的。特别是,两者都提供了一种将调用决策(在这种情况下调用哪个函数?)推迟到运行时(当编译时发生正常调用分派时)的方法。

That’s wrong because these are different levels of abstraction.

An analogy may help: saying that virtual functions and function pointers are identical is like saying that wheels and bikes are identical.

While it’s true that function pointers and virtual functions may look much the same “under the hood”, they are different things – both conceptionally (a virtual function is an overriable member method of a class while a function pointer is simply an indirection of a function) and syntactically (calling them is completely different).

They may serve the same purpose, however. In particular, both provide a means of deferring a calling decision (which function to call in this situation?) until runtime when normal call dispatching happens at compile time.

过期以后 2024-09-09 13:37:39

我想说接近,但不完全是。虚函数仍然是一个函数,但它通常通过指针调用,而不是直接调用。

I'd say close, but not quite. A virtual function is still a function, but it's normally called via a pointer, not directly.

流星番茄 2024-09-09 13:37:39

是的,虚拟函数表通常在底层实现为函数指针表。然而,还有其他硬件与指针表一起使这些函数实际上是“虚拟的”。你必须有一种机制来在运行时将调用绑定到正确的指针等。我这样说是因为认为虚拟函数是最基本级别的函数指针是错误的,这使得任何函数指针都是虚函数。

Yes, a virtual function table is often implemented under the hood as a table of function pointers. However, there is also other hardware to go along with the table of pointers to make the functions actually "virtual". You have to have a mechanism in place to bind a call to the correct pointer at run-time, etc. I say this because it would be wrong to think that since a virtual function is a function pointer at its most basic level that that makes any function pointer a virtual function.

淡淡的优雅 2024-09-09 13:37:39

其实C++支持虚函数,但C不支持VF,因为两者是完全不同的概念

Actually C++ Supports Virtual Functions , but C Does not supports VF because both are totally different concepts

佼人 2024-09-09 13:37:39

C++ 中的虚拟函数根据定义是使用关键字 virtual 声明的函数(直接声明或在基类之一中声明)。就这样。

现在,可以静态动态解析对虚拟函数的调用。动态解析的调用是根据调用中使用的对象的动态类型进行解析的调用。就这样。

上面没有任何内容引用任何“函数指针”。然而,在典型的实现中,为了实现动态调用的正确行为,使用具有函数指针(指向虚函数)的表。该表就是所谓的“VMT”、“VFT”或“vtable”。

换句话说,函数指针是一个实现细节,通常用于为动态调用虚拟函数提供支持。

为了进一步说明这一点,请注意,例如,即使某个函数是虚拟的,但它永远不会被动态调用,那么就不需要为该函数生成任何“指针”。因此,某些编译器不会为抽象类生成 VMT,因为即使这些类具有虚函数,但这些函数永远不会被动态调用。

Virtual function in C++ by definition is a function declared with keyword virtual (immediately or in one of the base classes). That's all.

Now, calls to virtual functions can be resolved statically or dynamically. A dynamically-resolved call is a call that resolved in accordance with dynamic type of the object used in the call. That's all.

Nothing in the above has any references to any "function pointers". However, in a typical implementation in order to implement the proper behavior of dynamic calls, a table with function pointers (pointing to virtual functions) is used. This table is what is known as "VMT", "VFT" or "vtable".

In other words, function pointer is an implementation detail typically used to provide support for dynamic calls to virtual functions.

To illustrate it further, note, for example, that even if some function is virtual, but it is never called dynamically, then there's no need to generate any "pointers" for that function. For this reason, some compilers do not generate VMTs for abstract classes, since even though these classes have virtual functions, these functions are never called dynamically.

丑丑阿 2024-09-09 13:37:39

我猜它来自《Understanding Linux Network Internals》一书——我们这里谈论的是 C,你的括号写错了——它是 virtual (function table),而不是 (virtual函数)表 :)。虚函数是 C++ 独有的术语。

意味着你不能用ANSI编码OOP ...

I guessed it's from the Understanding Linux Network Internals book -- We're talking about C here, and you've got your parenthesis wrong -- it's virtual (function table), not (virtual function) table :). Virtual functions are a C++ only term.

Which doesn't mean you can't code OOP in ANSI C...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文