Using C flag enums in C++

前端 未结 5 509
时光说笑
时光说笑 2020-12-16 15:51

I have a C API that defines an enum like so:

typedef enum
{
  C_ENUM_VALUE_NONE    = 0,
  C_ENUM_VALUE_APPLE   = (1 << 0),
  C_ENUM_VALUE_BANANA  = (1          


        
5条回答
  •  庸人自扰
    2020-12-16 16:37

    In the case of bitwise operations, the expression evaluates to a primitive type, i.e. int, long, etc. However, your function takes a non-primitive type (CEnumType). The only way I know of to bypass this is to cast the expression. For example:

    do_something((CEnumType) (C_ENUM_VALUE_APPLE | C_ENUM_VALUE_BANANA));
    

提交回复
热议问题