I have trouble seeing the utility of function pointers. I guess it may be useful in some cases (they exist, after all), but I can't think of a case where it's better or unavoidable to use a function pointer.
Could you give some example of good use of function pointers (in C or C++)?
For OO languages, to perform polymorphic calls behind the scenes (this is also valid for C up to some point I guess).
Moreover, they're very useful to inject different behaviour to another function (foo) at runtime. That makes function foo higher-order function. Besides it's flexibility, that makes the foo code more readable since it let's you pull that extra logic of "if-else" out of it.
It enables many other useful things in Python like generators, closures etc.
Like Rich said above, it is very usual for functions pointers in Windows to reference some address that stores function.
When you programming in
C language
on Windows platform you basically load some DLL file in primary memory(usingLoadLibrary
) and to use the functions stored in DLL you need to create functions pointers and point to these address (usingGetProcAddress
).References:
LoadLibrary
GetProcAddress
A different perspective, in addition to other good answers here:
In C, there are only function pointers, there aren't any functions.
I mean, you write functions, but you cant manipulate functions. There's no run-time representation of a function as such. You can't even call "a function". When you write:
what you're actually saying is "perform a call to the
my_function
pointer with the specified argument". You're making a call via a function pointer. This decay to function pointer means that the following commands are equivalent to the previous function call:and so on (thanks @LuuVinhPhuc).
So, you're already using function pointers as values. Obviously you would want to have variables for those values - and here is where all the uses other metion come in: Polymorphism/customization (like in qsort), callbacks, jump tables etc.
In C++ things are a bit more complicated, since we have lambdas, and objects with
operator()
, and even anstd::function
class, but the principle is still mostly the same.Agree with all of the above, plus.... When you load a dll dynamically at runtime you'll need function pointers to call the functions.
I am going to go against the current here.
In C, function pointers are the only way to implement customization, because there is no OO.
In C++, you can use either function pointers or functors (function objects) for the same result.
The functors have a number of advantages over raw function pointers, due to their object nature, notably:
operator()
lambda
andbind
)I personally prefer functors to function pointers (despite the boilerplate code), mostly because the syntax for function pointers can easily get hairy (from the Function Pointer Tutorial):
The only time I have ever seen function pointers used where functors could not was in Boost.Spirit. They have utterly abused the syntax to pass an arbitrary number of parameters as a single template parameter.
But since variadic templates and lambdas are around the corner, I am not sure we will use function pointers in pure C++ code for long now.
My main use of them has been CALLBACKS: when you need to save information about a function to call later.
Say you're writing Bomberman. 5 seconds after the person drops the bomb, it should explode (call the
explode()
function).Now there's 2 ways to do it. One way is by "probing" all bombs on the screen to see if they're ready to explode in the main loop.
Another way is to attach a callback to your clock system. When a bomb is planted, you add a callback to make it call bomb.explode() when the time is right.
Here
callback.function()
can be any function, because it is a function pointer.