本文介绍了C ++检查枚举标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个类中,我有一个整数,存储一组枚举标志,如:

 枚举AttackFlags 
{
Contact = 1,//移动与目标连接
Projectile = 2,//攻击是基于弹子的
Unblockable = 4,//攻击不能被阻止
UncounterableLv1 = 8,//攻击不能被反击除非极端的反攻击技能/状态效果
UncounterableLv2 = 16,//攻击不能被反击
Flinches = 32,//有机会眩晕敌人,推回他们的下一个回合
Unreflectable = 64,//攻击渗透反射。只检查魔法攻击
IgnoreDefenderStatusEffects = 128,//忽略防御者的活动状态效果
IgnoreAttackerStatusEffects = 256,//忽略攻击者的活动状态效果
IgnoreDefenderAbilities = 512,// Ignore防御者的能力
IgnoreAttackerAbilities = 1024,//忽略攻击者的能力
IgnoreArmorRating = 2048,//忽略装甲的防御提升
IgnoreWeaponRating = 4096,//忽略来自武器的攻击提升b $ b HighCritical = 8192,//移动碰撞的机会增加
CausesStatus = 16384,//移动会导致状态效果吗?
Elemental = 32768,//移动基于元素吗?
未实现= 65536,//移动已经实现了吗?
ModsTimer = 131072,//它对目标或用户计时器有影响吗?
Heals = 262144,//动作是否愈合?
SecondaryEffects = 524288,//攻击除了基本攻击之外还有额外的影响
PhysicalAttackFlag = 1048576,//是否是基于物理的类? I.E.被保护和屏蔽
MagicAttackFlag = 2097152,//是移动Magically Based吗? I.E.它是受壳的影响
MultiHit = 4194304,//它包装多于1命中
SingleUse = 8388608,//攻击只能每次战斗使用一次
DoesNotCauseDamage = 16777216
};

类攻击
{
int AtkFlags; // Stores AttackFlags |'d together

}

添加一个方法到我的Attack类中,具有以下签名

  bool HasAttackFlags 

标志将是众多AttackFlags |'。如果我只想检查一个标志,我可以只是& AtkFlags和flags在一起,但因为我必须检查多个可能的标志,这将不工作。如何正确检查多个标志?我想避免传递一个向量/集合的标志来检查一个简单的一组标志在一起更简单,然后构建一个向量/集



谢谢



要澄清我的意思,我可能会有以下

 攻击atk; 
atk.AtkFlags =联系我们射弹|治愈| MagicAttackFlag;

然后,我想检查atk上的标志,如下:

  bool res = atk.HasAttackFlags(Contact | Projectile); 

res应为true,反之亦然。

  bool res = atk.HasAttackFlags(Contact | Unreflectable); 

应该为false,因为AtkFlags不包含两者。Contact adn Unreflectable。

解决方案

我不确定我是否关注你的问题,因为你似乎提到了明显的解决方案。所以这是一个解决方案是错误的,显然添加任何额外的标志你希望:

  bool HasAttackFlags b $ b return(flags&(contact | projectile))!= 0; 
}

编辑:哦...我想我只是想出来,想要检查是否存在2个或更多的标志作为集合?在这种情况下,您可以简单地将方法修改为:

  bool HasAttackFlags(int flags){
return(flags& (contact | projectile))==(contact | projectile);
}


In one of my classes, I have an integer that stores a set of enum flags like so:

enum AttackFlags
{
    Contact = 1,                        //Move connects with target
    Projectile = 2,                         //Attack is projectile based
    Unblockable = 4,                        //Attack can not be blocked
    UncounterableLv1 = 8,                   //Attack can't be countered except by extreme counter attack skills/status effects
    UncounterableLv2 = 16,                  //Attack can not be countered
    Flinches = 32,                          //Has a chance to stun the enemy, pushing back their next turn
    Unreflectable = 64,                     //Attack penetrates reflect. Only checked for Magic attacks
    IgnoreDefenderStatusEffects = 128,      //Ignores active status effects on the defender
    IgnoreAttackerStatusEffects = 256,      //Ignores active status effects on the attacker
    IgnoreDefenderAbilities = 512,          //Ignore the defenders abilities
    IgnoreAttackerAbilities = 1024,         //Ignore the attackers abilities
    IgnoreArmorRating = 2048,               //Ignore the defensive boosts of armor
    IgnoreWeaponRating = 4096,              //Ignore the attack boost from weapons
    HighCritical = 8192,                    //The move has an increased chance to crit
    CausesStatus = 16384,                   //Can the move cause status effects?
    Elemental = 32768,                      //Is the move elemental based?
    Unimplemented = 65536,                  //has the move been implemented yet?
    ModsTimer = 131072,                     //Does it have an effect on the target or users timer?
    Heals = 262144,                         //Does the move heal?
    SecondaryEffects = 524288,              //Attack has additional effects besides basic attack
    PhysicalAttackFlag = 1048576,           //Is the Class Physically based? I.E. blocked by Protect and Shield
    MagicAttackFlag = 2097152,              //Is the move Magically Based? I.E. is it affected by things like Shell
    MultiHit = 4194304,                     //Does it enxcapsulate more then 1 hit
    SingleUse = 8388608,                    //Attack can only be used once per battle
    DoesNotCauseDamage = 16777216
};

class Attack
{
   int AtkFlags; //Stores AttackFlags |'d together

}

I'd like to add a method to my Attack class with the following signature

bool HasAttackFlags(int flags);

flags would be numerous AttackFlags |'d together. If I only wanted to check against a single flag, I could just & AtkFlags and flags together, but because I have to check against multiple possible flags this won't work. How can I properly check for multiple flags? I'd like to avoid passing a vector/set of flags to check against as simply |'ing a set of flags together is simpler then constructing a vector/set

Thanks in advance

EDIT:

To clarify what I mean, I might have the following

Attack atk;
atk.AtkFlags = Contact | Projectile | Heals | MagicAttackFlag;

Then later, I'd want to check the flags on atk like so:

bool res = atk.HasAttackFlags(Contact | Projectile);

res should be true and conversely

bool res = atk.HasAttackFlags(Contact | Unreflectable);

should be false becase AtkFlags does not contain both Contact adn Unreflectable.

解决方案

I am not sure I am following your question, because you seem to mention the obvious solution. So what is wrong with this as a solution, obviously add whatever additional flags you wish:

bool HasAttackFlags(int flags) {
    return (flags&(contact|projectile))!=0;
}

EDIT: Oh... I think I just figured it out, do you want to check for the presence of 2 or more flags as a set? In which case you can simply modify the method to:

bool HasAttackFlags(int flags) {
    return (flags&(contact|projectile))==(contact|projectile);
}

这篇关于C ++检查枚举标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:41