Callback Functions in C++
A callback function in C++ is a function that is passed as an argument to another function. The receiving function can then call (or “callback”) the provided function at a specific point, allowing for dynamic execution of behavior. Callback functions are widely used in event-driven programming, asynchronous operations, and modular designs.
Callbacks in C++ can be implemented using function pointers, functors (function objects), or lambda expressions.
Types of Callback Functions
- Function Pointer Callback: A traditional method where a function pointer is passed as the callback.
- Functor Callback: A callback implemented using a class or struct with an overloaded
operator()
. - Lambda Expression Callback: An inline, anonymous callback function defined using a lambda expression.
Examples
Example 1: Callback Using Function Pointer
This example demonstrates how to implement a callback using function pointers.
</>
Copy
#include <iostream>
using namespace std;
void onSuccess(int result) {
cout << "Callback: Operation successful, result = " << result << endl;
}
void performOperation(int a, int b, void (*callback)(int)) {
int sum = a + b;
callback(sum); // Call the callback function
}
int main() {
performOperation(5, 10, onSuccess); // Pass the callback function
return 0;
}
Output:
Callback: Operation successful, result = 15
Explanation:
- The
onSuccess
function is defined as a callback that takes an integer and prints it. - The
performOperation
function takes two integers and a function pointer as arguments. It calculates the sum of the integers and calls the provided callback function with the result. - In
main
, the address ofonSuccess
is passed toperformOperation
, allowing it to be executed dynamically.
Example 2: Callback Using Functor
This example demonstrates how to implement a callback using a functor.
</>
Copy
#include <iostream>
using namespace std;
class SuccessCallback {
public:
void operator()(int result) const {
cout << "Functor Callback: Result = " << result << endl;
}
};
void performOperation(int a, int b, const SuccessCallback& callback) {
int sum = a + b;
callback(sum); // Call the functor
}
int main() {
SuccessCallback callback;
performOperation(20, 30, callback);
return 0;
}
Output:
Functor Callback: Result = 50
Explanation:
- The
SuccessCallback
class defines an overloadedoperator()
, making it callable like a function. - The
performOperation
function accepts an instance of the functor as a parameter and calls it with the computed result. - In
main
, an instance ofSuccessCallback
is created and passed toperformOperation
, which invokes the callback.
Example 3: Callback Using Lambda Expression
This example demonstrates how to implement a callback using a lambda expression.
</>
Copy
#include <iostream>
using namespace std;
void performOperation(int a, int b, const function<void(int)>& callback) {
int product = a * b;
callback(product); // Call the lambda
}
int main() {
performOperation(7, 6, [](int result) {
cout << "Lambda Callback: Result = " << result << endl;
});
return 0;
}
Output:
Lambda Callback: Result = 42
Explanation:
- The
performOperation
function accepts astd::function
object as a callback. - A lambda expression is passed to
performOperation
that prints the result of the computation. - Inside
performOperation
, the lambda is called with the product of the numbersa
andb
. - The result is dynamically handled by the inline lambda function.
Key Points to Remember about Callback Functions
- Callback functions allow for dynamic and flexible code execution.
- Function pointers, functors, and lambda expressions are common ways to implement callbacks in C++.
- Callbacks are widely used in event-driven programming, asynchronous operations, and modular designs.
- Using
std::function
for callbacks provides additional flexibility and compatibility with modern C++.