Operations on enumerations

I'm used to writing enums for enumerated types. However I have this:
1
2
3
4
5
6
7
8
enum Colour
{
  BLUE  = 0x01,
  BLACK = 0x02,
  WHITE = 0x04,
  GREEN = 0x08,
  RED   = 0x10
};


Now each of the enumerated types are not exclusive, so I'd like to do this:
Colour c = BLUE | BLACK;

Of course that doesn't work. gcc gives "invalid conversion from 'int' to 'Colour' [-fpermissive]". Would you suggest this instead?

1
2
3
4
5
6
typedef int Colour;
static const Colour BLUE = 0x01;
static const Colour BLACK = 0x02;
static const Colour WHITE = 0x04;
static const Colour GREEN = 0x08;
static const Colour RED   = 0x10;


Otherwise, I'm very open to an ingenious way to encapsulate it.
You can
a) define logical operators for your enum
b) use integers
c) use std::bitset
d) use a new class type with logical operators defined

(incidentally, the first three are the options for what C++ calls "bitmask types": http://en.cppreference.com/w/cpp/concept/BitmaskType )
Last edited on
Thanks. Never knew I could define logical operators for my enum. Hope I don't need to define a name for each combination.
Topic archived. No new replies allowed.