我正在尝试将 FastDelegate 转换为 std::function,但我无法理解语法。
这是委托(delegate)库: http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible
以及我要转换的代码:
typedef shared_ptr<IEventData> IEventDataPtr;
typedef fastdelegate::FastDelegate1<IEventDataPtr> EventListenerDelegate;
//some code to find the event
EventListenerDelegate listener = (*it);
listener(pEvent); // call the delegate
到目前为止这还行不通:
typedef std::function<std::shared_ptr<IEventData>> Functor;
Functor listener = (*it);
listener(pEvent); // call the delegate
最佳答案
这可能不是一个完整的答案,因为除了提供回调功能之外,我不完全确定 fastDelegate 库支持做什么。 那里的原始代码来自 Mike Shaffry 和 Dave Graham 的 Game Coding Complete。
所以我可能遗漏了书中所说的 fastDelegate 库可以附加成员变量的部分。因此,由于您有一个以 IEventData 作为参数的 void 函数,因此您的所有回调函数都将相同。
typedef std::function<void(IEventData)> Functor;
Functor listener = (*it);
listener(pEvent);
然后在您的解决方案中的其他地方定义实际的“Functor”,它只是 std::function 的 typedef。如:
void GameCode::DelegateFunction(IEventData){
//do stuff with event
}
这几乎就是它工作原理的要点。我知道这篇文章已经过时了,但我想我还是会回答,因为我自己是在凌晨 4 点才弄清楚的
关于c++ - 将快速委托(delegate)转换为标准函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37954180/