C++ 委托(delegate)创建

标签 c++ delegates

我想知道是否有办法在 C++ 中模仿这种委托(delegate)行为(来自 C#)

new ModifyTargetingStatus("Reversal", "Reverses physical attacks back at the user", -1, new List<CALL_CONDITION>(){CALL_CONDITION.Targetted}, attemptChange: delegate(CharacterMove move, BattleCharacter[] users, ref BattleCharacter[] targets, BattleCharacter holder, BattleField field)
                {
                    if (targets != null && targets.Length == 1 && targets[0] == holder &&
                        (move.MoveClass == MOVE_CLASS.MC_BASIC_ATTACK ||
                        move.MoveClass == MOVE_CLASS.MC_STACKED_ATTACK) &&
                        !move.MoveFlags.Contains(MOVE_FLAG.MF_UNREFLECTABLE))
                    {
                        targets = users;
                        move.Reflected = true;
                        return true;
                    }
                    return false;
                })

我在代码项目上查看了 boost::function 和 boost::bind 以及一篇关于 FastDelegates 的文章,但它们似乎都要求您在其他地方定义函数然后绑定(bind)到它。如果可能的话,我希望能够在对象创建时提供该函数

提前致谢

最佳答案

这些称为匿名函数 (lambda),C++ 不支持它们。来自 wikipedia article :“自 C++0x 起,C++ 语言中已添加匿名函数。”

关于C++ 委托(delegate)创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5237542/

相关文章:

c++ - 在 C++ 中流入和流出文本文件

python - Python 的 C 和 C++ 库如何跨平台?

c++ - int main(int argc, char *argv[])

c# - 无法将方法组 '' 转换为非委托(delegate)类型 'System.Delegate'。您是否打算调用该方法?

c# - 为什么 C# 中的委托(delegate)语法允许 +=,而总是最后一个函数也会被指向

winforms - 具有启动、暂停和停止事件处理的动态线程

c++ - 奇怪的 cout 行为

android - 链接到 Android 上的 ceres 库

c# - 方法参数似乎是动态的,不确定它是如何工作的

ios - 如何让多个 View 使用同一个 Controller 类?