Does it make sense to typedef an enum used for bit masks?
I've seen code where they are typedef'd, but doing so means I'd have to overload bitwise operators for every enum and it's a bit annoying
Is there any advantage in declaring a mask variable with its own type instead of just using an int?
"Does it make sense to typedef an enum used for bit masks?"
The use of enumerations is favoured over the use of pre-processor constants. But why would you type-define an enumeration? Wouldn't the initial identifier suffice?
maeriden wrote:
"Is there any advantage in declaring a mask variable with its own type instead of just using an int?"
Yes: any instantiations of an enumeration can only be assigned to enumerators that pertain to the enumeration. For example:
Only the enumerators of LockFlags can be assigned to Lock. If you had an ordinary int, masks could conflict by causing overlapping of the masks. Of course, this is caused by careless programming. If a constant variable/object's value is known at compiler time, the compiler doesn't have to allocate memory for it.
By the way, int isn't always the underlying type of an enumeration, because the type used is implementation defined.