Re: pointers to functions
BigBrian wrote:
What about something like this....
#include <iostream>
//yuck! using namespace std;
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
//int minus (int,int) = subtraction;
typedef int(op)(int,int);
BigBrian was the first person to suggest using a typedef!
A great idea (if the OP's training has already covered this
concept, which unfortunately we don't know), but he didn't
complete the thought...
op * minus = subtraction;
Good, but...
int operation (int x, int y, int (*functocall)(int,int))
This should be
int operation (int x, int y, op*functocall)
{
int g;
g = (*functocall)(x,y);
And this would be a lot easier to read as
g = functocall(x,y);
return (g);
}
Or better yet, as someone already pointed out:
int operation(int x, int y, op*func) { return func(x,y); }
int main ()
{
int m,n;
m = operation (7, 5, addition);
n = operation (20, m, minus);
std::cout <<n;
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]