Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 100421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:34:34+00:00 2026-05-11T00:34:34+00:00

I have this enum: enum ButtonState { BUTTON_NORMAL = 0, BUTTON_PRESSED = 1, BUTTON_CLICKED

  • 0

I have this enum:

enum ButtonState {     BUTTON_NORMAL = 0,     BUTTON_PRESSED = 1,     BUTTON_CLICKED = 2 };  const u8 NUM_BUTTON_STATES = 3; 

In my Button class I have member variables ButtonState state; and ButtonColors colors[NUM_BUTTON_STATES];. When drawing the button, I use colors[state] to get the colours for whatever state the button is in.

My questions:

  1. Is this good programming style? Is there a better way to do it? (I usually only use enums with switch statements… using an enum as an array index doesn’t feel right.)
  2. Do I have to specify the values of the enum? It seems to start from 0 by default and increment by 1 but is it guaranteed to work that way in all compilers?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:34:35+00:00Added an answer on May 11, 2026 at 12:34 am

    Is this good programming style?

    I think so. I do the same thing quite frequently.

    Is there a better way to do it?

    class Button { public:     // Used for array indexes!  Don't change the numbers!   enum State {     NORMAL = 0,     PRESSED,     CLICKED,     NUMBER_OF_BUTTON_STATES   }; }; 

    Drawback is that NUMBER_OF_BUTTON_STATES is now a valid Button::State value. Not a big issue if you are passing these values around as ints. But trouble if you are actually expecting a Button::State.

    Using an enum as an array index doesn’t feel right.

    It’s fine. Just DOCUMENT it, so the next guy knows what’s going on! (That’s what comments are for.)

    Do I have to specify the values of the enum?

    With no ‘=’ assignment, enum’s are supposed to start at zero and increment upwards.

    If a enum entry has an ‘=’ assigned value, subsequent non ‘=’ enum entries continue counting from there.

    Source: The Annotated C++ Reference Manual, pg 113

    That said, I like to specify the initial value just to make the code that much clearer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 57k
  • Answers 57k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You could use a look-ahead assertion: (?!999)\d{3} This example matches… May 11, 2026 at 8:20 am
  • added an answer If your NOTES.DAT file is stored on an NTFS partition,… May 11, 2026 at 8:20 am
  • added an answer Because the second int x = 4; Is attempting to… May 11, 2026 at 8:20 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Related Questions

Loading...

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.