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 
onSuccessfunction is defined as a callback that takes an integer and prints it. - The 
performOperationfunction 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 ofonSuccessis 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 
SuccessCallbackclass defines an overloadedoperator(), making it callable like a function. - The 
performOperationfunction accepts an instance of the functor as a parameter and calls it with the computed result. - In 
main, an instance ofSuccessCallbackis 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 
performOperationfunction accepts astd::functionobject as a callback. - A lambda expression is passed to 
performOperationthat prints the result of the computation. - Inside 
performOperation, the lambda is called with the product of the numbersaandb. - 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::functionfor callbacks provides additional flexibility and compatibility with modern C++. 
