In C++11 you can use enum class
:
enum class door_t { closed = 0, open = 1 };
But then you have to refer to door_t::open
instead of simply open
. This has the advantages and disadvantages of verboseness. enum class
is good in these cases where a name conflict is likely, and it effectively adds a generic prefix to the enumerators of the enumeration.
(Actually, in C++11 you don't need to use the class
keyword. Simply referring to door_t::open
resolves the conflict with no other change.)
You can arrive at a similar solution in C++03 by putting an enum
inside a class
(hence the syntax of the C++11 feature)
struct door_state {
enum door_t { closed = 0, open = 1 };
};
You can make this a nested class, or define it before washm_t
and use it as a base class
class washm_t : public door_state {
int open();
};
Now the the member open()
overrides the enumerator, but the enumerator is still available as door_state::open
, and closed
is still available using the same syntax as before.