CC_CALLBACK之间的区别

#define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__)
#define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
#define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
#define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, ##__VA_ARGS__)

①__selector__:绑定要回调的函数名,注意要加上命名空间:函数名函数

②__target__:绑定一个对象this

③std::placeholders::_1:绑定的函数里除了第一个参数以外的参数,就是调用函数传参时要传第一个参数,若是_selector_后面有参数,则在用CC_CALLBACK时要绑定spa

④std::placeholders::_2:依次类推code

CC_CALLBACK的做用是让_target_对象用_selector_函数时绑定第0/1/2/3个参数后面参数的值,例如对象

int add (int i, int j){

return i+j;

}

auto func = CC_CALLLBACK_1(add, this, 10);

这时获得的func就至关与func(int i, int j = 10),用的时候能够用func(15),blog

cout << func(15) << endl; // 结果是15 + 10 = 25,即cout << 25
相关文章
相关标签/搜索