Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 除了这一长串的“问题”之外,还有别的选择吗;如果argA==argB do…“;?_C++_Oop_Enums - Fatal编程技术网

C++ 除了这一长串的“问题”之外,还有别的选择吗;如果argA==argB do…“;?

C++ 除了这一长串的“问题”之外,还有别的选择吗;如果argA==argB do…“;?,c++,oop,enums,C++,Oop,Enums,编辑:由于attack.condition偶尔会有多个值,switch语句将不起作用 因此,我有一个枚举,它将增长: enum Condition { Null = 0x0001, SelfIsUnderground = 0x0002, SelfIsGround = 0x0004, SelfIsAir = 0x0008, Sel

编辑:由于attack.condition偶尔会有多个值,switch语句将不起作用

因此,我有一个枚举,它将增长:

enum Condition {    Null            =   0x0001,
            SelfIsUnderground   =   0x0002,
            SelfIsGround        =   0x0004,
            SelfIsAir       =   0x0008,
            SelfIsWater     =   0x0010,
            OtherIsUnderground  =   0x0020,
            OtherIsGround       =   0x0040,
            OtherIsAir      =   0x0080,
            OtherIsWater        =   0x0100,
            Tile            =   0x0200,
            CONDITION_COUNTX    =   0x03FF};
这一功能也将增长:

bool Attack::CanBeDone(Spirit* pSelf, Spirit* pTarget,Map* pMap)
{
    if(this->condition!=Null)
    {
        if(this->condition & SelfIsUnderground)
            if(pSelf->GetcurrentLayer()!=Underground)
                return false;

        if(this->condition & SelfIsGround)
            if(pSelf->GetcurrentLayer()!=Ground)
                return false;

        if(this->condition & SelfIsAir)
            if(pSelf->GetcurrentLayer()!=Air)
                return false;

        if(this->condition & SelfIsWater)
            if(pSelf->GetcurrentLayer()!=Water)
                return false;

        if(this->condition & OtherIsUnderground)
            if(pTarget->GetcurrentLayer()!=Underground)
                return false;

        if(this->condition & OtherIsGround)
            if(pTarget->GetcurrentLayer()!=Ground)
                return false;

        ...
除了一遍又一遍地写,还有别的选择吗

    if(this->condition & arg)
        if(pSelf->GetcurrentLayer()!=value)
            return false;
?


奖励:如果我给条件::Null值0x0000,SelfIsUnderground 0x0001,SelfIsGround 0x0002,并再次使用2的幂,它会工作吗?最后,Tile将以0x0100的值结束。

首先,我将这样写:

enum Condition {    Null            =   0x0001,
            SelfBase        =   0x0002,
            SelfIsUnderground   =   SelfBase * (1 << Underground),
            SelfIsGround        =   SelfBase * (1 << Ground),
            SelfIsAir       =   SelfBase * (1 << Air),
            SelfIsWater     =   SelfBase * (1 << Water),
            SelfMask        =   SelfBase * ((1 << Max) - 1),
            OtherBase       =   0x0020,
            OtherIsUnderground  =   OtherBase * (1 << Underground),
            OtherIsGround       =   OtherBase * (1 << Ground),
            OtherIsAir      =   OtherBase * (1 << Air),
            OtherIsWater        =   OtherBase * (1 << Water),
            OtherMask       =   OtherBase * ((1 << Max) - 1),
            Tile            =   0x0200,
            CONDITION_COUNTX    =   0x03FF};

这在扩展枚举时仍然正确。但是请注意,仍然存在一些冗余(例如,如何定义
OtherBase
Tile
),这些冗余也可以减少。静态断言有助于确保
条件
枚举定义良好。

如果将
条件
枚举绑定在一起,则可以在以下行中写入内容:

enum Condition {    Null            =   0x0001,
    SelfIsUnderground   =   0x0002,
    SelfIsGround        =   0x0004,
    SelfIsAir       =   0x0008,
    SelfIsWater     =   0x0010,
    OtherIsUnderground  =   0x0020,
    OtherIsGround       =   0x0040,
    OtherIsAir      =   0x0080,
    OtherIsWater        =   0x0100,
    Tile            =   0x0200,
    CONDITION_COUNTX    =   0x03FF};

enum Layer
{
    Ground = SelfIsGround,
    Underground = SelfIsUnderground,
    Air = SelfIsAir,
    Water =  SelfIsWater
};

struct Spirit
{
    Layer GetcurrentLayer() const;

};

struct Map;

struct Attack
{
    Condition condition;
    bool CanBeDone(Spirit* pSelf, Spirit* pTarget,Map* pMap)
    {
        return (condition & pSelf->GetcurrentLayer() )&&
             ( (condition / 0x10) & pTarget->GetcurrentLayer());
    }

};

更可能的是,我希望您根据
层定义
条件
,但这只是细节。

这是您通过使用位掩码和按位操作进行过早优化得到的结果;没有简单的选择

为什么不
std::vector selfLayers,otherLayers
?(或者您不能有多个层-您的代码暗示您可以。)
或者使用从图层到所需条件位的映射

另外,我不明白为什么
攻击
类为两个
精灵
存储条件。谁首先设置
Attack::condition


首先,我不会对非位的对象使用位图,也不会对非枚举的对象使用枚举。我会考虑花一些时间来创建简单的类(例如:代码>层< /COD>,<代码>条件< /代码>)和助手函数(<代码>攻击::GETSoSeCeleAs),我太困惑了,无法确定。我还将检查我的类关系,以确保
攻击
确实需要
条件
标志等。

我会说完全放弃
条件
枚举,而是使用结构。自我和目标的条件是分开的,应该这样对待

enum Layer : char //only takes 1 byte
{
    Ground      = 1<<0,
    Underground = 1<<1,
    Air         = 1<<2,
    Water       = 1<<3
};
struct Condition {
    Layer Self;
    Layer Target;
};
bool Attack::CanBeDone(Spirit* pSelf, Spirit* pTarget,Map* pMap) {
    return this->Condition.Self & pSelf->GetcurrentLayer() 
        && this->Condition.Target & pTarget->GetcurrentLayer();
}
enum层:char//只需要1字节
{

Ground=1我可以用我的奖励积分买什么?我没有提到“积分”这个词:p您是否可以循环查看
枚举
值?请参见:您要查找的是
开关
语句吗?@l19仅在这种情况下有效,因为未编号的枚举值会增加,在这里OP会给它们显式的位图值。
N*(1@BenVoigt:我发现我的版本比,比如说,
SelfBase更容易理解。我喜欢你所做的。时间很晚了,我会去睡觉,但我会在明天详细看看。谢谢。你能用枚举中定义的值来表达硬编码的
0x10
吗?否则,你必须记住在增强代码时更新此代码枚举。@krlmr。当然,在真正的代码中,你可以按你喜欢的方式来封装它。@krlmr。请阅读你的答案。我并不是在说什么。提示:
>0x10
有点过火。多少钱是
OtherIsUnderground/SelfIsUnderground
?——你的解决方案只是实现相同结果的另一种方式,有一些优势阶段和一些缺点。层的作用比我的问题中所显示的要大得多。因此,我不能按你说的去做。层的作用比我的问题中所显示的要大得多。因此,我不能按你说的去做。
enum Layer : char //only takes 1 byte
{
    Ground      = 1<<0,
    Underground = 1<<1,
    Air         = 1<<2,
    Water       = 1<<3
};
struct Condition {
    Layer Self;
    Layer Target;
};
bool Attack::CanBeDone(Spirit* pSelf, Spirit* pTarget,Map* pMap) {
    return this->Condition.Self & pSelf->GetcurrentLayer() 
        && this->Condition.Target & pTarget->GetcurrentLayer();
}