C ++委托创建 [英] C++ delegate creation
问题描述
new ModifyTargetingStatus(Reversal ,Reverses physical attacks back to the user,-1,new List< CALL_CONDITION>(){CALL_CONDITION.Targetted},attemptChange:delegate(CharacterMove move,BattleCharacter [] users,ref BattleCharacter [] targets,BattleCharacter holder,BattleField字段)
{
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关于代码项目的文章,但他们似乎都需要你在其他地方定义函数然后绑定到它。
提前感谢
这些被称为匿名函数(lambdas),并且在C ++中不受支持。从维基百科文章: < href =http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions =nofollow> C ++ 0x
I was wondering if theres a way to mimic this delegate behaviour (from C#) in 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;
})
I've looked at boost::function and boost::bind and an article about FastDelegates on code project, but they all seem to require you to define the function somewhere else then bind to it. I'd like to be able to provide the function at object creation if possible
Thanks in advance
These are called anonymous functions (lambdas), and aren't supported in C++. From the wikipedia article: "Anonymous functions were added to the C++ language as of C++0x."
这篇关于C ++委托创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!