0

I have theenumerated type, colors:

enum colors {green, red, blue};

Is colors mycolors=red the same as int yourcolors=red and is the type of each enumerator int? The both will have a value of 1, right?

Thanks!

  • 1
    enums are implemented as numerics (which will probably be int, but may be smaller), however keeping them in a narrow range close to zero could allow the compiler more optimizations (e.g. the compiler might decided they can be crammed into a char if that helps). The values are in fact the same internally, but you're better off using the enums whenever possible as they abstract away the underlying numerical types. Also be aware that other related languages are more strict on this than C++. You can interchange ints and enums in C++, but not in C# (even though it will be stored as a numeric). – Jason Lang May 06 '17 at 22:22
  • 3
    Just use `enum class` if you're coding with C++11. – Daniel Kamil Kozar May 06 '17 at 22:23
  • 3
    The type of `mycolors` is `colors`, the type of `yourcolors` is `int`. – Kerrek SB May 06 '17 at 22:23
  • Try `bool mycolors = red;` and `double mycolors = red`, also 1. – takintoolong May 06 '17 at 22:32
  • @DanielKamilKozar, why? And thanks Jason for the explanation! –  May 06 '17 at 22:47

2 Answers2

2

I just want to post a little code snippet to prove the comments of Jason Lang and Kerrek SB:

#include <iostream>
#include  <typeinfo>
enum colors {green, red, blue};

int main()
{   
    colors mycolors=red;
    int yourcolors=red;
    if (mycolors == yourcolors)
        std::cout << "same values" << std::endl;

    if (typeid(mycolors) != typeid(yourcolors))
        std::cout << "not the same types" << std::endl;

    return 0;
}

Running this code will lead into the following console output:

same values
not the same types

Also (as Daniel Kamil Kozar mentioned) there is enum class (only C++11 and later!). See this Question for more information about why to prefer enum class over enum.

Regarding the question 'why are enums after not just ints (or longs or ...) just think of operator overloading. That is ++ colors(green) == 1 must not be true. Confirm this Question that operator overloading is possible for plain enums and this question and the accepted answer to see how to avoid casting in overloading operators of an 'enum class'.

At last keep in mind that the usage of enums - if used reasonable - improves code readability.

Community
  • 1
  • 1
BlameTheBits
  • 850
  • 1
  • 6
  • 22
  • Well, thanks! But if "mycolors == yourcolors" is true, does that mean that the value of mycolors is an int(1) and if this is true, couldn't the variable just be of type int instead of colors –  May 06 '17 at 22:54
  • It seems that the fact that both variables have the same values may implies that both variables are of the same type - but they are not. Just think of `0.0 == 0.0f` but a `double` is not the same as a `float`. (Probably not the best example but I cannot find a better one right now.) – BlameTheBits May 06 '17 at 23:07
  • @Game Ideas: II totally forgot operator overloading. I added a paragraph about it. – BlameTheBits May 07 '17 at 07:52
1
  • I think enum seems a little more type-safety. You can do int yourcolors=red, but not colors mycolors=1.
  • When I'm debugging enum usage is helpful. It shows enumeration name instead of its value.
  • Enumeration values aren’t lvalues. So, when you pass them by reference, no static memory is used. It’s almost exactly as if you passed the computed value as a literal.

enum KEYS
{
    UP,
    RIGHT,
    DOWN,
    LEFT
};

void (KEYS select)
{
    switch (select)
    {
        case UP:
        case RIGHT:
        case DOWN:
        case LEFT: break;
        default: exit(1);
    }
}
  • Well thanks. So with these advantages states, it is more about legibility and to make your code more efficient, when it comes to whether you should use enum or int? Or am I wrong? –  May 06 '17 at 22:58
  • I have added an example. I'd prefer to see the labels instead of numbers because of its readability. – Soner from The Ottoman Empire May 07 '17 at 06:34