CALLBACK example

Below is the example of callback function between two classes which is present in same file,they can be part of different library,diffrent file etc and can be attached to different processes.Basically client(class a) registers a function with server so that when server want to call,it can call back that function.Here the callback function wrapper_process registered with server(class b).

The code provided below is compiled in linux and also output is provided here.


class b


static b *ptr;
static bool instanceFlag;


static b* func();

void *s;
void(*t)( void *u,int y);
void tx()
std::cout<<"calling tx method of object b"; t(this,20); } void registery(void *p, void(*q)(void *r, int x)) { s=p; t=q; } }; /* b* b::ptr = NULL; bool b::Flag = false; static b::b* func() { std::cout<<"static func called"; if(!b::Flag) { ptr = new b(); instanceFlag = true; return ptr; } else { return ptr; } }*/ bool b::instanceFlag = false; b* b::ptr = NULL; b* b::func() { if(! instanceFlag) { ptr = new b(); instanceFlag = true; return ptr; } else { return ptr; } } extern "C" { static void wrapper_process(void *pq,int z); } extern "C" { static void wrapper_process(void *rs,int zs) { printf("%d--got value from",zs); } } class a{ public: a() { b *z = b::func(); z->registery(this,wrapper_process);

int main(){

//b *sc1 = b::func();
a ax;
b *sc1 = b::func();

return 0;


hellocalling tx method of object b20–got value from

Leave a Reply