gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:04:47 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com