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 << 1),
C_ENUM_VALUE_COCONUT = (1 << 2),
// etc.
C_ENUM_VALUE_ANY = ~0
} CEnumType;
There is a method that uses the enum, defined as:
void do_something(CEnumType types);
In C, you can call something like:
do_something(C_ENUM_VALUE_APPLE | C_ENUM_VALUE_BANANA);
However, if you try to call it this way in C++ (Linux, g++ compiler), you get an error, invalid conversion from ‘int’ to ‘CEnumType’.
What is the correct way to use this C API from my C++ application?
You need to cast
ints to enums in C++, but you can hide the cast in a customORoperator:With this operator in place, you can write your original
and it will compile and run without a problem.