C++ pointers to functions and callbacks
How to create and use function pointers, including using them in callbacks
As with a data object, the code generated for a function body is placed in memory, so it has an address which means we can have a pointer to it.
Unlike data object pointers, function pointers do not allows us to modify the function code. There are only two things we can do with a function pointer: call it and take its address.
The pointer obtained by taking the address of a function can be used to call the function.
Declaring and using a pointer to a function
In the example code bellow a pointer is set with the address of a function and then used to call the function.
|
Pointers to functions have arguments declared just like the functions themselves. That’s why the pointer fp
was declared as returning void
and with a string
parameter.
Note that using *
for deferencing and &
to get the address of a function is optional. Consider the following code example.
|
Function pointers and callbacks
A typical use for function pointers is to pass a callback function as the parameter of another function.
Imagine you have a function that should call another function, let’s call this other function the callback, which is provided as a parameter. By having the callback function as a parameter of the first function allows to call different functions on different calls of the first function. Confused? Don’t worry, it will become clearer in the code example.
A way to implement the callback mechanism is by using a function pointer that is passed as a parameter of another function. Let’s look at a code example.
|
Use cases to this mechanism is in asynchronous programming, where you want to run some code when some event happens, and in libraries.