C++, class with method and enum type - can method and enum value have same names? -
i have following class:
class washm_t { public: enum door_t { closed = 0, open = 1 }; private: door_t door_state ; public: int open() ; };
the problem class open
name defined both name of method inside of class , value of enum type.
the question is: can somehow modify code both open
names work inside of class or should pick other name open()
method or open
enum type value?
this how open()
function looks like:
int washm_t::open() { if(door_state == open) // ~fails, 'open' interpreted name of method // not name of enum type value return 1 ; else return door_state = open, 0 ; }
in c++11 can use enum class
:
enum class door_t { closed = 0, open = 1 };
but have refer door_t::open
instead of open
. has advantages , disadvantages of verboseness. enum class
in these cases name conflict likely, , adds generic prefix enumerators of enumeration.
(actually, in c++11 don't need use class
keyword. referring door_t::open
resolves conflict no other change.)
you can arrive @ similar solution in c++03 putting enum
inside class
(hence syntax of c++11 feature)
struct door_state { enum door_t { closed = 0, open = 1 }; };
you can make nested class, or define before washm_t
, use base class
class washm_t : public door_state { int open(); };
now the member open()
overrides enumerator, enumerator still available door_state::open
, , closed
still available using same syntax before.
Comments
Post a Comment