Virtual function == function pointer?

A set of function pointers grouped into a data structure often called a virtual function table (VFT).

The above statement makes me feel like virtual function == function pointer , right?

+4
source share
7 answers

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

In C ++, virtual functions are defined using v-table . And entries in vtable can be implemented as pointers to functions.

+5
source

This is wrong because these are different levels of abstraction.

An analogy can help: saying that virtual functions and function pointers are identical, they say that wheels and bicycles are identical.

Although it is true that pointers to functions and virtual functions can look something like β€œunder the hood,” they are different things β€” as conceptually (a virtual function is a method of an overloaded class member, and a function pointer is just an indirect function) and syntactically (calling them completely different).

However, they can fulfill the same goal. In particular, both provide a way to defer the decision on a call (which is called in this situation?) Until runtime, when normal dispatching of calls occurs during compilation.
+2
source

I would say close, but not quite. A virtual function is still a function, but it is usually called via a pointer, not directly.

+1
source

Yes, a virtual function table is often implemented under the hood as a table of function pointers. However, there is other hardware to go along with a table of pointers to make virtual functions actually. You must have a mechanism to bind the call to the correct pointer at runtime, etc. I say this because it would be wrong to think that since a virtual function is a pointer at the most basic level, what any function pointer does is a virtual function.

+1
source

Actually, C ++ supports virtual functions, but C does not support VF, because both are completely different concepts.

+1
source

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

Now virtual function calls can be resolved statically or dynamically. A dynamically resolved call is a call that is resolved according to the dynamic type of the object used in the call. It's all.

Nothing in the above links to any "function pointers". However, in a typical implementation, to implement the correct behavior of dynamic calls, a table with function pointers (pointing to virtual functions) is used. This table is called "VMT", "VFT" or "vtable".

In other words, a function pointer is an implementation detail commonly used to support dynamic calls to virtual functions.

To illustrate this further, note, for example, that even if a function is virtual, but it is never called dynamically, then there is no need to create any β€œpointers” for this function. For this reason, some compilers do not generate VMT for abstract classes, because although these classes have virtual functions, these functions are never called dynamically.

+1
source

I guessed that from the book Understanding Linux Network Internals - we are talking about C here, and you have the wrong brackets - this is virtual (function table) , not (virtual function) table :). Virtual functions are just a C ++ term.

Which does not mean that you cannot encode OOP in ANSI C ...

0
source

Source: https://habr.com/ru/post/1310654/


All Articles