views:

317

answers:

7

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?

A: 

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

Jerry Coffin
+3  A: 

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.

Brian R. Bondy
But you can emulate them with a structure of function pointers; it's quite a common design used in object-oriented projects that use C.
Matteo Italia
@Matteo: Ya I know, you can emulate OO in general simply by having the first parameter of a group of functions take a pointer of your struct. I meant language support.
Brian R. Bondy
Certainly; actually my comment wasn't a negation of your answer, just a clarification. :)
Matteo Italia
+1  A: 

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.

Konrad Rudolph
The quoted statement is not wrong. A set of function pointers grouped into a data structure is often referred to as a virtual function table (in C) or a vtable (if associated with a C++ object). A vtable is one implementation mechanism for virtual functions, and a virtual function is at a different level of abstraction from a function pointer rather than being at a different level of composition.
Pete Kirkham
@Pete Kirkham: Yes but the quoted statement doesn’t equal function pointers and virtual functions.
Konrad Rudolph
So why is the first line of your answer 'the statement is wrong', rather than 'your feeling about the statement is not what it meant'
Pete Kirkham
@Pete Kirkham: I meant the OP’s assertion. Whoops, I just noticed how misleading that is.
Konrad Rudolph
A: 

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

Kornel Kisielewicz
Yes it's from the book.But `virtual (function table)` and `(virtual function) table` seem the same thing,huh?
httpinterpret
since when red (apple box) is the same as (red apple) box?
Kornel Kisielewicz
A: 

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.

Donnie
A: 

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

c4learn.com
A: 

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.

AndreyT