c++ - 将快速委托(delegate)转换为标准函数

标签 c++ c++11 std

我正在尝试将 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/

相关文章:

c++ - std::move 在 boost 库中的对应物

c++ - 初始化 std::unique_ptr 作为原始数组指针初始化

c++ - 如何在 GDB 调试器命令行中调用 std::function 句柄

c++ - 如果我们不显式声明一个命名空间名称,全局变量属于什么命名空间?

c++ - shared_ptr 不会在程序结束时破坏对象

c++ - 将 std::wstring 转换为 const wchar_t x[]

c++ - 使用智能指针或引用包装器定义自定义 map

arrays - std::arrays 的 std::vector 的比较函数

c++ - 函数中模板类的返回值不能去掉 "warning not all control paths return a value"

c++ - 关于 c++ 中没有逻辑运算符的 while(n)