开发者

virtual function == function pointer?

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?


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.


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.


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


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.


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜