我一直认为在 C ++ 和 C 中,enum
都是整数。但是今天我得到了“从int
到E
的无效转换”:
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;
}
答案 0 :(得分:0)
您应该强制转换它,因为它的类型为int,这样就可以实现您的目标
E first = FIRST;
E third = static_cast<E>(FIRST | SECOND);