我想知道是否有办法在 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/