If you decide to use enum for your flags, here is a useful macro that creates code for bitwise operators for your enum type.
#define GENERATE_ENUM_FLAG_OPERATORS(enumType) \
inline enumType operator| (enumType lhs, enumType rhs) \
{ \
return static_cast<enumType>(static_cast<int>(lhs) | static_cast<int>(rhs)); \
} \
inline enumType& operator|= (enumType& lhs, const enumType& rhs) \
{ \
lhs = static_cast<enumType>(static_cast<int>(lhs) | static_cast<int>(rhs)); \
return lhs; \
} \
inline enumType operator& (enumType lhs, enumType rhs) \
{ \
return static_cast<enumType>(static_cast<int>(lhs) & static_cast<int>(rhs)); \
} \
inline enumType& operator&= (enumType& lhs, const enumType& rhs) \
{ \
lhs = static_cast<enumType>(static_cast<int>(lhs) & static_cast<int>(rhs)); \
return lhs; \
} \
inline enumType operator~ (const enumType& rhs) \
{ \
return static_cast<enumType>(~static_cast<int>(rhs)); \
}
Usage:
enum Test
{
TEST_1 = 0x1,
TEST_2 = 0x2,
TEST_3 = 0x4,
};
GENERATE_ENUM_FLAG_OPERATORS(Test);
Test one = TEST_1;
Test two = TEST_2;
Test three = one | two;
bool
. – Vainglorious|
,&
and '~'. – Chorea