#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__:綁定一個對象
③std::placeholders::_1:綁定的函數里除了第一個參數之外的參數,就是調用函數傳參時要傳第一個參數,如果_selector_后面有參數,則在用CC_CALLBACK時要綁定
④std::placeholders::_2:依次類推
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),
cout << func(15) << endl; // 結果是15 + 10 = 25,即cout << 25