Author Topic: function pointers? (i think)  (Read 2905 times)

0 Members and 1 Guest are viewing this topic.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
function pointers? (i think)
« 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.

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: function pointers? (i think)
« Reply #1 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);   

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: function pointers? (i think)
« Reply #2 on: April 15, 2007 »
Thanks Emil.