[英]How do I use tr1 function and bind for functions with changing parameters?
I'm currently going from C# to C++ and rewriting some game engine code and I think I'm at a wall with tr1; 我目前正在从C#转到C ++,并重写一些游戏引擎代码,我想我与tr1隔墙相望。 Essentially what I want to do is have an input layer take input from the touchscreen and then fire a callback to notify any elements listening to that input. 本质上,我想做的是让一个输入层从触摸屏上获取输入,然后触发一个回调以通知所有侦听该输入的元素。 Now tr1/boost seem to make this super easy via the use of function and bind , however firing callbacks that are void() is no problem, but now I'm trying to send parameters through to these callback when invoke (enums for instance). 现在tr1 / boost似乎通过使用function和bind使此超级简单,但是触发void()的回调没问题,但是现在我试图在调用时将参数发送给这些回调(例如枚举) 。
The problem here is that when I call bind, and insert the parameter, it doesn't act as a place holder rather it acts as the permanent value to be passed. 这里的问题是,当我调用bind并插入参数时,它不充当占位符,而是充当要传递的永久值。 For instance my code here: 例如我的代码在这里:
Containing class: 包含类:
tr1::function<void (MenuActions action)> callback;
callback = tr1::bind(&MenuScene::handleInputCallback, this, MenuActions::neutral);
m_menuUILayer->registerCallback(callback);
Has the intent that I create a function and bind it then registerCallback passes this callback into the member class and stores the reference. 我打算创建一个函数并绑定它,然后registerCallback将此回调传递给成员类并存储引用。 This part works, but now in the member class I can invoke the callback however the default parameters are always sent... 这部分有效,但是现在在成员类中我可以调用回调,但是始终发送默认参数...
Member class: 会员等级:
if (m_callback)
{
m_callback(MenuActions::backgroundTouchDown);
}
So here rather than MenuActions::backgroundTouchDown being sent to the main containing class in the callback, the default MenuActions::neutral is still being used. 因此,这里不是将MenuActions :: backgroundTouchDown发送到回调中的主包含类,而是仍在使用默认的MenuActions :: neutral 。 I'm wondering if I'm missing something, or perhaps my lack of sleep is just sending me down the wrong path? 我想知道我是否想念某些东西,或者也许我缺乏睡眠只是在误导我? Thanks guys! 多谢你们!
You should use placeholders 您应该使用占位符
callback = tr1::bind(&MenuScene::handleInputCallback, this,
tr1::placeholders::_1);
and then call like 然后像
if (m_callback)
{
m_callback(MenuActions::backgroundTouchDown);
}
and your function handleInputCallback
can have default parameter, defaulted to MenuActions::neutral
并且您的函数handleInputCallback
可以具有默认参数,默认为MenuActions::neutral
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.