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
Function Pointers and Callbacks in C++
A callback is a function that is passed as an argument to another function. In C++, we cannot directly use the function name to pass them as a callback. We use function pointers to pass callbacks to other functions, store them in data structures, and invoke them later. In this article, we will discuss how to use function pointer to pass callbacks in C++ programs.