d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ / C In Line Callbacks?
Add Reply New Topic New Poll
Member
Posts: 6,325
Joined: Dec 2 2007
Gold: 2,347.75
Feb 18 2013 03:04pm
Hey..

I am trying to figure out how I can make some kind of callback on my function.

Lets say i have a function like so:

Code
void callBackTester (int i, int j){

   for(; i < j; i++){
       //Let callback know what i is now.
       //callBack(i);
   }
}


And when I call the function i want it to be like this.:

Code
callBackTester(0, 10) {
   printf("%d", i);
}


I can do this with preprocessor macros, but those can't be recursive which i need it to be..

This post was edited by Utunity on Feb 18 2013 03:06pm
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Feb 18 2013 06:41pm
You need to pass the callback as an argument.
In C that means a function pointer, in C++ you can also pass stateful callbacks.

Here's how you'd do it in plain C: http://ideone.com/4vswzC

Here's a stateful C++ example: http://ideone.com/aWqVzM

This post was edited by KrzaQ2 on Feb 18 2013 06:50pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll