Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: Stonemonkey on April 15, 2007

Title: function pointers? (i think)
Post by: Stonemonkey on April 15, 2007
Hi all, say i had something like:

typedef struct
{
  whatever operation;
}my_struct;

int add(int a,int b)
{
  return a+b;
}

int subtract(int a,int b)
{
  return a-b;
}


and to call it i want to do something like:


//test.operation=either add or subtract;
result=test.operation(a,b);

How would i go about something like that?

Cheers, Fryer.
Title: Re: function pointers? (i think)
Post by: Emil_halim on April 15, 2007
Code: [Select]

typedef int (CALLBACK *Function)(int,int);

Function myfunc;

int addFunc(int a,int b)
{
  return a+b;
}

int subtract(int a,int b)
{
  return a-b;
}

//test.operation=either add or subtract;
if(operation == add)
   myfunc = addFunc;
else
   myfunc = subtract;   
   
// call our desired one
result=myfunc(a,b);   
Title: Re: function pointers? (i think)
Post by: Stonemonkey on April 15, 2007
Thanks Emil.