How to create a Callback function by Using C/C++
Solution 1
I don't know C++, I wrote up some C code, hope it helps
#include <stdio.h>
void call( int, void ( *f )( int ) );
void called( int );
int main( int argc, char const *argv[] ){
printf( "start\n" );
call( 1, called );
printf( "end\n" );
getchar();
return 0;
}
void call( int a, void ( *f )( int ) ){
printf( "doing stuff\n" );
printf( "a: %d\n", a );
printf( "going to call back\n" );
f( a * 2 );
}
void called( int b ){
printf( "b: %d\n", b );
printf( "b*2: %d\n", b*2 );
printf( "call back function being called\n" );
}
Calling call back functions in a function is no more than having a function pointer and call the function whenever you finished your planed job.
I modified the code and made it up to be more clear to show how you would use call backs.
This is the result you will see, if you compile and run it:
start
doing stuff
a: 1
going to call back
b: 2
b*2: 4
call back function being called
end
Well, either the author edited the question or someone edited it for him, there's no longer the C tag. Ignore this answer if you don't want to see pure C code. I'm just gonna leave it here in case anyone could be interested.
Solution 2
Well, you never called "callee" - you executed "callback" with "call(void*)call2);", but in order for your "callee" function to execute, you need to run it from within your "callback" function. Here, this works:
#include <iostream>
#include <stdio.h>
using namespace std;
void callee()
{
printf("callee\n");
printf("calleeeeeeeeee\n");
}
void callback(void* func)
{
void (*mCallbackFunc)(void);
printf("callback\n");
mCallbackFunc = (void(*)())func;
mCallbackFunc();
}
int main()
{
void (*call)(void*);
void (*call2)(void);
call2 = &callee;
call = &callback;
call((void*)call2);
return 0;
}
Output:
callback
callee
calleeeeeeeeee
![Yoshua Joo Bin](https://i.stack.imgur.com/xfnqV.png?s=256&g=1)
Yoshua Joo Bin
Updated on June 14, 2022Comments
-
Yoshua Joo Bin about 2 years
does C\C++ Support a mechanism for callback function ? and how to create it ? I've written several code, to create callback function in C++ but it failed ..
#include<iostream> using namespace std; void callee() { printf("callee\n"); printf("calleeeeeeeeee\n"); } void callback(void* func) { printf("callback\n"); } int main() { void (*call)(void*); void (*call2)(void); call2 = &callee; call = &callback; call((void*)call2); getchar(); return 0; }
-
Yoshua Joo Bin over 11 yearshmm ouh i have to execute the function that is passed into parameter, right ?
-
Yoshua Joo Bin over 11 yearsNice :), i got that :) Thank you :)
-
enhzflep over 11 yearsBingo! That's exactly it. Operations with function pointers are often easier to read if you typedef the function pointer first, but it's just a matter of looking pretty. As you point out - you need to explicitly do something with the passed in parameter - in this case, execute the function it points to.
-
loulou almost 11 years@prM, +1. What do you mean by "whenever you finished your planed job"? Thanks!
-
pochen almost 11 years@user1847726 err, just a function call (to the given function pointer) statement at the very end of the function body.