Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 882
httpinterpret Avatar asked Dec 22 '22 02:12

httpinterpret


2 Answers

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.

like image 142
Brian R. Bondy Avatar answered Jan 07 '23 07:01

Brian R. Bondy


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.

like image 30
Konrad Rudolph Avatar answered Jan 07 '23 07:01

Konrad Rudolph