0

i have searched a little information about "callback function",but i think i didn't understand the true meaning of that.And i write some code to practice.

Here is the code

void *callbackfunction(int m1,void*d1)
{
cout<<"called CBF()  "<<m1<<endl;
return (void*) callbackfunction;
}

void accpt(int m1,void*m2(int m3,void*d1))
{
char a='a';
m2(m1,&a);
callbackfunction(m1,&a);
cout<<m1<<" "<<endl;
}

void c()
{
void (*callback)(int,void*callbackfunction(int,void*));
callback=accpt;
callback(1, callbackfunction);
}

int main(int argc, const char * argv[])
{
c();
return 0;
}

Is't just simply that make accpt call callbackfunction ,why not just call it ,and do not use function pointer?

m2(m1,&a);
callbackfunction(m1,&a);

Does callback function have else advantage,or my perspective is wrong? pls help me

林子勤
  • 29
  • 6
  • 1
    I literally googled your title and that was the first result. – Barry Jul 16 '15 at 19:25
  • The example is deliberately oversimplified. Callbacks are for use by custom iterators, event despatches, etc. So that your own code can be 'fired' as part of a known iteration sequence or when a signal is raised. Your callback would be input into another object as a funcptr and wait to be invoked. – underscore_d Jul 16 '15 at 19:28

0 Answers0