Function Pointers and Callbacks in C++
In C++, a callback is a function passed to another function using a function pointer, as function names can't be passed directly. This allows storing and invoking functions dynamically.
Function Pointer to a Callback
To create a function pointer to any particular callback function, we first need to refer to the signature of the function. Consider the function foo()
int foo(char c) {
.......
}
Now the function pointer for the following function can be declared as:
int (*func_ptr)(char) = &foo;
This function pointer allows you to call the function foo() indirectly using func_ptr. We can also pass this function pointer to any other function as an argument allowing the feature of callbacks in C++.
C++ Program to Illustrate the Use of Callback
// C++ program to illustrate how to use the callback
// function
#include <iostream>
using namespace std;
// callback function
int foo(char c) { return (int)c; }
// another function that is taking callback
void printASCIIcode(char c, int(*func_ptr)(char))
{
int ascii = func_ptr(c);
cout << "ASCII code of " << c << " is: " << ascii;
}
// driver code
int main()
{
printASCIIcode('a', &foo);
return 0;
}
Output
ASCII code of a is: 97