I'll just add some things to Disch's comment
Function pointers can not only be declared like void (*func)();
it's more like, the left parameter (void) tells you the return parameter of func.
the second brackets are used to give parameters
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
int add(int val1, int val2)
{
return val1 + val2;
}
int mult(int val1, int val2)
{
return val1 * val2;
}
int main(void)
{
int (*func)(int, int) = add;
for(int i = 0; i < 50; ++i)
{
std::cout << "result: " << func(i, i) << std::endl;
// dynamically switching where it points to
if(func == add) func = mult;
else func = add;
}
}
|
So, when declaring, you tell the return type and the parameter types.
since you are familiar with std::function, it has something like this hidden somewhere inside, and uses templates too.
std::function<int(int, int)> func;
the return parameter is int, and the passed parameter list contains of int and int (bad example but I'll stick to it :b)
It basically equals this:
int (*func)(int, int);
but just... better ^^