For some reason, the left-hand operand of the assignment is returning an "int", and not a "Flag" enumerator. Why is this? and how can I overcome this? I tried casting the result of the negation to a "Flag" enumerator, but it still gave the same error.
If you were able to do that, it could not be guaranteed that the value of Flags was set to one of the valid enum values (FLAG_LOCKED, etc.). Why not make Flags to an (unsigned) integer type?
@kevinkjt2000: As I said, I tried casting the result of the negation to a "Flag" enumerator, but the compiler wouldn't allow it; it gave the same error. However, enumerators are not always of type "int", because the underlying type is implementation-defined (though, the type must be integral).
@Peter87: Hmmmmm... using "unsigned" isn't that bad of an idea, and it may only be the only choice I have right now. I'll leave this thread open a bit longer to see if anyone suggests anything else.
The compiler shall issue an error because there is no overloaded operator &= for the enumeration type.
You can overload this operator for your enumeration. For example
1 2 3 4 5 6 7 8 9 10 11
enum X { x, y };
X & operator &=( X &lhs, int rhs )
{
return ( lhs = static_cast<X>( lhs & rhs ) );
}
int main()
{
X a = X::y;
a &= ~X::y;
}
Never mind, lads. I ended up implementing a structure which holds static constants which represent the bit masks, overloads the bitwise operators, and so forth. So far it works as expected. In fact, it works better than an enumeration list.