std::bind
#include<iostream>
#include<functional>
using namespace std;
int add(int i, int j){
return i + j;
}
int main(){
auto func = std::bind(add, placeholders::_1, placeholders::_2);
cout << func(1, 2);
return 0;
}
//output:3
绑定就是将一个函数绑定,上面的例子中,placeholder是占位符,表示func接收的参数将会使用到add方法中,如果我们将站位符改成固定的数字:
#include<iostream>
#include<functional>
using namespace std;
int add(int i, int j){
return i + j;
}
int main(){
auto func = std::bind(add, 1, 2);
cout << func();
return 0;
}
//output:3
std::bind还可以绑定成员函数,静态函数,不过其不能绑定重载函数。
CC_CALLBACK_X
CC_CALLBACK_X在cocos2dx中的定义:
// new callbacks based on C++11
#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__)
实际的效果就是让target调用绑定的selector回调函数,并绑定第0-3个参数后面参数的值。
参考资料:
1.CC_CALLBACK之间的区别
2. C++11 std::bind std::function 高级用法