Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Incrementing and enum?

Incrementing and enum?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++tutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stick
    wrote on last edited by
    #1

    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.

    M 1 Reply Last reply
    0
    • S Stick

      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.

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups