I’ve learned that putting enums in namespaces avoids compiling errors when two enums share one equaly named item.
namespace Feeling
{
enum e
{
Happy = 1,
Sad = 2,
Blue = 4,
Angry = 8,
Mad = 16
};
}
So you can pass it to functions that are declared like
void HowDoYouFeel(Feeling::e feeling);
But when trying to OR it like this:
HowDoYouFeel(Feeling::Happy | Feeling::Blue);
I get an error. What is the best way to handle this?
The question asks for “the best methd”, which is a bit subjective. I write
e operator|(e left, e right) { return e(int(left)|int(right)); }for these cases, to make clear thatFeeling::ehas an orthogonal encoding.