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

  1. Function Pointer Callback: A traditional method where a function pointer is passed as the callback.
  2. Functor Callback: A callback implemented using a class or struct with an overloaded operator().
  3. 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:

  1. The onSuccess function is defined as a callback that takes an integer and prints it.
  2. 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.
  3. In main, the address of onSuccess is passed to performOperation, 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:

  1. The SuccessCallback class defines an overloaded operator(), making it callable like a function.
  2. The performOperation function accepts an instance of the functor as a parameter and calls it with the computed result.
  3. In main, an instance of SuccessCallback is created and passed to performOperation, 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:

  1. The performOperation function accepts a std::function object as a callback.
  2. A lambda expression is passed to performOperation that prints the result of the computation.
  3. Inside performOperation, the lambda is called with the product of the numbers a and b.
  4. The result is dynamically handled by the inline lambda function.

Key Points to Remember about Callback Functions

  1. Callback functions allow for dynamic and flexible code execution.
  2. Function pointers, functors, and lambda expressions are common ways to implement callbacks in C++.
  3. Callbacks are widely used in event-driven programming, asynchronous operations, and modular designs.
  4. Using std::function for callbacks provides additional flexibility and compatibility with modern C++.