1.回调函数:本质是通过定义一个函数指针,来指向一个的函数。当调用这个函数指针(可以作为参数传递)的时候,其实调用的是指向的真正的函数实现,这就是回调函数。2.struct的回调函数使代码更清晰明了。
#include
#include
#include
#define MPH_MAX 10typedef void (*CharHook)(char *self);
typedef void (*IntHook)(int cout);void real_fun1(char *name){printf("xxx-------->%s(), line = %d, name = %s\n",__FUNCTION__,__LINE__,name);
}void real_fun2(int cout){printf("xxx-------->%s(), line = %d, cout = %d\n",__FUNCTION__,__LINE__,cout);
}//way01:
struct fun_ops{ //定义回调函数structvoid (*fun1)(char *self); //注册参数void (*fun2)(int cout);
};fun_ops fuops = {//初始化函数指针的函数.fun1 = real_fun1,.fun2 = real_fun2,
};int main(){fun_ops *ops = &fuops;//赋值结构体//1.way01ops->fun1(const_cast("Hello Callback"));//2.way02IntHook tst;tst = ops->fun2;tst(1234);
}
#include
#include
#include
#define MPH_MAX 10typedef void (*CharHook)(char *self);
typedef void (*IntHook)(int cout);void real_fun1(char *name){printf("xxx-------->%s(), line = %d, name = %s\n",__FUNCTION__,__LINE__,name);
}void real_fun2(int cout){printf("xxx-------->%s(), line = %d, cout = %d\n",__FUNCTION__,__LINE__,cout);
}// //way02:
typedef struct fun_ops{ //定义回调函数structCharHook fun1;IntHook fun2;
}fun_ops;fun_ops fuops = {//初始化函数指针的函数.fun1 = real_fun1,.fun2 = real_fun2,
};int main(){fun_ops *ops = &fuops;//赋值结构体//1.way01ops->fun1(const_cast("Hello Callback"));//2.way02IntHook tst;tst = ops->fun2;tst(1234);
}
上一篇:Python实现简单的换脸术