Looks like you need only the function itself to be passed. You've almost got it. Here is the correct syntax:
typedef double (*functype)();
void foo(functype f) {
cout << f();
}
double bar() {
return 2.39;
)
foo(bar); // don't call bar here, just pass it as address.
You can also declare foo as follows:
void foo(double (*f)());
Functions are not first-class objects in C++, so the only way to create a closure with some variables (for example, if you want to carry the function or call a non-static member function) is functors (objects with overloaded operator() ). There are several ways of getting such object:
- Define it by yourself, store all necessary variables in fields and pass it as function's argument.
- Use C++11's lambda functions and std::function
- Use boost::function and boost::bind.