Notes

Thoughts, write-ups, and things worth remembering.

Callback functions

#cpp#functional Updated Sep 16, 2026

A function passed as an argument to another function, to be called later — usually in response to something happening (a click, a finished computation, a sort comparison) rather than called directly by the code that received it.

Three ways to pass one, all valid

// 1. Function pointer - the original mechanism
void onComplete(int result) { std::cout << result; }
void doWork(void (*callback)(int));
doWork(onComplete);

// 2. Lambda - most common today
doWork([](int result) { std::cout << result; });

// 3. std::function - when the callback needs to be stored, not just passed through
std::function<void(int)> savedCallback = [](int result) { std::cout << result; };

See Function pointers and Lambda expressions for the mechanics of each — a callback isn’t a fourth language feature, it’s a role any of these three can play.

The defining pattern: called by someone else, later

void doWork(std::function<void(int)> onDone) {
    int result = expensiveComputation();
    onDone(result);   // the caller's code runs HERE, inside doWork - not
                       // where doWork itself was originally called from
}

The caller hands over what to do; doWork decides when to actually do it. doWork never needs to know or care what the callback actually does — that’s the whole point.

Where this shows up across the vault

  • std::sort’s comparator (see Lambda expressions) is a callback — sort calls it repeatedly; you never call it yourself.
  • Qt signals and slots is a callback mechanism with extra infrastructure bolted on (multiple listeners, loose coupling between sender and receiver).
  • The Observer pattern is what a callback looks like once “many listeners” gets formalized into its own class structure.

Function pointers
Lambda expressions
Qt signals and slots
Design patterns