I don't like having enum variables holding values that are not
enums. That's a hack.
It's a design feature of C++ enums. A C++ enum is not (just) an
enumerated type. I agree that the language would be clearer if
it distinguished between enumerated types and bitmap types, but
it doesn't. The keyword enum is used for both.
And I simply think that the right way to do that is a class.
Why not? It doesn't add any overhead, neither does any wrong.
If you're going that route (and I can understand it), then
wouldn't the "correct" solution be something like:
class Leds
{
public:
static Leds const redLed ;
static Leds const greenLed ;
static Leds const blueLed ;
Leds() ; // all off.
Leds( Leds const& other ) ;
Leds& operator|=( Leds const& other ) ;
Leds& operator&=( Leds const& other ) ;
freind Leds operator~( Leds const& op ) ;
private:
unsigned int myValue ;
explicit Leds( unsigned int value ) ;
} ;
Leds operator|( Leds const& lhs, Leds const& rhs ) ;
Leds operator&( Leds const& lhs, Leds const& rhs ) ;
Internally, you use bits and a bit mask on myValue, but all the
user sees is some values which he can manipulate:
Leds l1 ;
l1 |= redLed ;
Leds l2( l1 | greenLed ) ;
It seems a bit heavy to me, but it's certainly the most
"correct" in the abstract sense. And if you need to do it a
lot... it would take about ten minutes to write an AWK script to
generate the class, given the class name and a list of its
(constant) values.