C ++委托创建 [英] C++ delegate creation

查看:134
本文介绍了C ++委托创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆