C++11 std::bind和CC_回调_0~3)

C++11 std::bind和CC_回调_0~3),c++11,cocos2d-x,cocos2d-x-3.0,cocos2d-x-3.x,C++11,Cocos2d X,Cocos2d X 3.0,Cocos2d X 3.x,请原谅我糟糕的英语 标题文件 CCMAGL.H >代码> < COSCO2D-X 3 .x,CCI回调技术< /C> >使用新 ISO C++标准STD::BIN < /C> >,例如: #define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__) #define CC_CALLBACK_1(__selector__,__target__, ..

请原谅我糟糕的英语

标题文件<代码> CCMAGL.H >代码> < COSCO2D-X 3 .x,CCI回调技术< /C> >使用新<代码> ISO C++标准STD::BIN < /C> >,例如:

#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__)
//HelloWorld.cpp

    class HelloWorldScene : cocos2d::Layer
{
public :
    static cocos2d::Scene* createScene();
    virtual bool init();
    CREATE_FUNC(HelloWorldScene);
    // overload this function
    void ontouchmoved(cocos2d::Touch*, cocos2d::Event*);
};
我发送这个函数“HelloWorld::ontouchmoved”和指针“this”,“HelloWorld::ontouchmoved”是CC_回调_2中的选择器,“this”是CC_回调_2中的目标 但是为什么呢?我不再向CC_CALLBACK_2发送更多参数,但CC_CALLBACK_2的定义是:

    bool HelloWorldScene::init()
{
    auto listener = cocos2d::EventListenerTouchOneByOne::create();

    listener->onTouchBegan = [](cocos2d::Touch* _touch, cocos2d::Event* _event){ CCLOG("onTouchBegan..."); return true;};
    // using the CC_CALLBACK 
    listener->onTouchMoved = CC_CALLBACK_2(HelloWorldScene::ontouchmoved, this);
    listener->onTouchEnded = [](cocos2d::Touch* _touch, cocos2d::Event* _event){ CCLOG("onTouchEnded...");};
    cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

    return true;
}
void HelloWorldScene::ontouchmoved(cocos2d::Touch* touch, cocos2d::Event* event)
{
    CCLOG("onTouchMoved...");
}
占位符_1和placehoders_2没有绑定任何参数,它们有什么用途?我猜它们绑定了HelloWorld::ontouchmoved的参数,但我不知道如何绑定HelloWorld::ontouchmoved的参数


帮帮我!非常感谢

我不太明白你的问题,但为什么不使用lambda函数呢

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

请阅读AddEventListenerWithCeneGraphPriority函数中的更多详细信息。此时添加了触摸和事件。

std::placeholder::\N的目的是允许std::bind创建一个参数与原始函数的第N个参数匹配的函数

如果要创建采用较少参数(环绕原始函数)的函数,或重新排序或复制原始参数,则可以附加值而不是占位符。您还可以绑定通过值或引用传入的任何变量

如果你想了解更多关于你能做的疯狂的事情,你会想阅读更多关于std::bind的内容


很抱歉,我的陈述不清楚。我必须重新声明我的困惑:例如:listener->OnTouchMove=CC\u CALLBACK\u 2(HelloWorld::onTouchMoved,this);正如我们所看到的,成员函数“ontouchmoved”和指针“this”被传递给CC_回调_2,“HelloWorld::ontouchmoved”用于_选择器_;“this”对于uuu target_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu2,没有更多的参数传递给CC\u CALLBACK\u2,但是std::placeholder:1和std::placeholder::2在CC\u CALLBACK\u2中,所以,我猜std::placeholder:::::::::\u2是无用的,除了“HelloWorld::ontouchmoved”和“this”之外,没有其他参数传递给CC_CALLBACK_2。因此,std::placeholder::_1和std::placeholder::_2代表什么数据?为什么他们代表触摸和事件?
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);

listener->onTouchBegan = [&](Touch* touch, Event* event){

};

listener->onTouchMoved = [&](Touch* touch, Event* event){

};

listener->onTouchEnded = [&](Touch* touch, Event* event){

};

listener->onTouchCancelled = [&](Touch* touch, Event* event){

};

_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);