1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
using namespace std;
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
int (*minus)(int,int) = subtraction;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
|
main()
19: declare two variables of type int
20: declare one variable of type pointer to a function that looks like int func(int, int). Initialise it with address of function subtraction.
22: call function operation, pass parameters 7, 5, address of function addition
23: call function operation, pass parameters 20, m, minus (which is the address of subtraction)
operation()
10: operation is a function that takes 3 parameters, x of type int, y of type int, functocall of type pointer to a function that looks like int func(int, int)
12: declare variable g of type int
13: call the function who's address in in local variable functocall, with parameters x, y and save the result in g
This really should be:
14: return g