virtual function == function pointer?
Asked Answered
H

7

5

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?

Hube answered 23/5, 2010 at 12:40 Comment(0)
R
5

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.

Raving answered 23/5, 2010 at 12:43 Comment(3)
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.Whorl
@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.Raving
Certainly; actually my comment wasn't a negation of your answer, just a clarification. :)Whorl
R
2

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.

Roaster answered 23/5, 2010 at 12:47 Comment(4)
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.Monarchism
@Pete Kirkham: Yes but the quoted statement doesn’t equal function pointers and virtual functions.Roaster
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'Monarchism
@Pete Kirkham: I meant the OP’s assertion. Whoops, I just noticed how misleading that is.Roaster
R
1

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

Radioactivity answered 23/5, 2010 at 12:43 Comment(0)
W
1

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.

Washery answered 23/5, 2010 at 13:2 Comment(0)
C
1

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

Covenanter answered 23/5, 2010 at 13:56 Comment(0)
W
1

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.

Went answered 23/5, 2010 at 17:14 Comment(0)
K
0

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

Kim answered 23/5, 2010 at 12:49 Comment(2)
Yes it's from the book.But virtual (function table) and (virtual function) table seem the same thing,huh?Hube
since when red (apple box) is the same as (red apple) box?Kim

© 2022 - 2024 — McMap. All rights reserved.