C ++中不是只枚举整数吗?

时间:2019-02-01 17:26:01

标签: c++ enums integer

我一直认为在 C ++ C 中,enum都是整数。但是今天我得到了“从intE的无效转换”:

enum E {
    FIRST = 0b01,
    SECOND = 0b10,
    THIRD = FIRST | SECOND  // this is fine
};

int main()
{
    E first = FIRST;
    E third = FIRST | SECOND; // this is NOT fine

    return 0;
}

1 个答案:

答案 0 :(得分:0)

您应该强制转换它,因为它的类型为int,这样就可以实现您的目标

E first = FIRST;
E third = static_cast<E>(FIRST | SECOND);