Function Pointers
Declaring a function pointer
double add(double a, double b) { return a + b; }
double (*op)(double, double) = add; // or &add
op(2, 3); // calls add → 5
The parentheses around *op are required: double* op(double, double) would declare a function returning double*.
Cleaner with using
using BinaryOp = double (*)(double, double);
BinaryOp op = add;
Passing functions as arguments (callbacks)
double apply(double a, double b, BinaryOp f) { return f(a, b); }
apply(6, 3, multiply);
Arrays/tables of function pointers
Dispatch tables replace long switch statements:
BinaryOp ops[] = { add, subtract, multiply, divide };
ops[choice](a, b);
Classic use: comparison functions
bool descending(int a, int b) { return a > b; }
std::sort(v.begin(), v.end(), descending);
Limitations
- Can't capture local state (no context)
- Awkward syntax
- Member function pointers have a different, even stranger syntax (
&Class::method,(obj.*ptr)())
Modern alternatives ✅ (Unit 14)
- Lambdas:
[](int a, int b) { return a > b; } std::function<double(double, double)>: can hold function pointers, lambdas (with captures) and functors- Templates: accept any callable with zero overhead
Function pointers are still common when working with C libraries (e.g. qsort, signal handlers, GUI callbacks).
Example
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
double add(double a, double b) { return a + b; }
double subtract(double a, double b) { return a - b; }
double multiply(double a, double b) { return a * b; }
double divide(double a, double b) { return b != 0 ? a / b : 0; }
using BinaryOp = double (*)(double, double);
double apply(double a, double b, BinaryOp f) { return f(a, b); }
bool descending(int a, int b) { return a > b; }
bool by_last_digit(int a, int b) { return a % 10 < b % 10; }
void on_event(const std::string& name, void (*callback)(const std::string&)) {
std::cout << "Event '" << name << "' happened -> ";
callback(name);
}
void notify(const std::string& e) { std::cout << "notification sent for " << e << '\n'; }
void log_it(const std::string& e) { std::cout << "logged " << e << '\n'; }
int main() {
double (*op)(double, double) = add; // raw syntax
std::cout << "op(2, 3) = " << op(2, 3) << '\n';
op = &multiply; // & is optional
std::cout << "op(2, 3) = " << op(2, 3) << '\n';
std::cout << "apply(10, 4, subtract) = " << apply(10, 4, subtract) << '\n';
BinaryOp table[] = { add, subtract, multiply, divide };
const char symbols[] = { '+', '-', '*', '/' };
for (int i = 0; i < 4; ++i)
std::cout << "12 " << symbols[i] << " 4 = " << table[i](12, 4) << '\n';
std::vector<int> v = { 42, 7, 93, 15, 68 };
std::sort(v.begin(), v.end(), descending);
std::cout << "descending:";
for (int x : v) std::cout << ' ' << x;
std::sort(v.begin(), v.end(), by_last_digit);
std::cout << "\nby last digit:";
for (int x : v) std::cout << ' ' << x;
std::cout << '\n';
on_event("budget exceeded", notify);
on_event("expense added", log_it);
return 0;
}
/* Output:
op(2, 3) = 5
op(2, 3) = 6
apply(10, 4, subtract) = 6
12 + 4 = 16
12 - 4 = 8
12 * 4 = 48
12 / 4 = 3
descending: 93 68 42 15 7
by last digit: 42 93 15 7 68
Event 'budget exceeded' happened -> notification sent for budget exceeded
Event 'expense added' happened -> logged expense added
*/