Incrementing and enum?
-
I have a switch which has positions defined like thie: class InstLights { public: enum RHEO{ OFF, ON25, ON50, ON75, BRT }; static void IncRheoLevel(); private: static RHEO rheoLevel; } And basically I want to be able to call IncRheoLevel() like this: InstLights::IncRheoLevel(); and thought it would be clean & neat to do it like this: // in .cpp file void InstLights::IncRheoLevel(){ rheoLevel++; } however, that would require operator overloading (operator++) which would require a non-static object. For reasons I won't go into here, I need to use a static object (there is only one anyway). So, my question is... given a current rheoLevel, what is the best way to "increment" it? While I could just easily use int's, I want to try and understand how to use enums better. Thanks.
-
I have a switch which has positions defined like thie: class InstLights { public: enum RHEO{ OFF, ON25, ON50, ON75, BRT }; static void IncRheoLevel(); private: static RHEO rheoLevel; } And basically I want to be able to call IncRheoLevel() like this: InstLights::IncRheoLevel(); and thought it would be clean & neat to do it like this: // in .cpp file void InstLights::IncRheoLevel(){ rheoLevel++; } however, that would require operator overloading (operator++) which would require a non-static object. For reasons I won't go into here, I need to use a static object (there is only one anyway). So, my question is... given a current rheoLevel, what is the best way to "increment" it? While I could just easily use int's, I want to try and understand how to use enums better. Thanks.
There isn't a built-in increment (or decrement) ability for enums because you can do this:
enum Bob { foo=1, bar=5, baz };
The values don't have to be consecutive. You can cast between an enum and an int, however it's up to you to make sure you only use valid values when casting to an enum type.
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?