enums??

Pages: 12
Could someone explain what are enums, and how do we use them?
Enums are deprecated, and you should not use them.

Enum Classes (aka Scoped Enums), however, are not deprecated, and are a way to create an enumeration of a limited number of possible values for new type.
1
2
3
4
5
6
enum struct EnemyType
{
    Ground,
    Flying,
    Ghost
};
The above enum class creates a new data type, known as EnemyType, which may only take on one of the values listed.

Enum classes can also be of a specific data type, read more here:
http://en.cppreference.com/w/cpp/language/enum
Last edited on
thanks @L B
is it like typedef?
can i create a alpha numeric data type with it?
Enumerators can be found at the bottom of this page:
http://www.cplusplus.com/doc/tutorial/other_data_types/

Typically, whenever you find yourself giving meaning to a number ( such as one equal January ), then you should consider using an enumerator.

You can also create "options" using enumerators, you see this a lot in windowing APIS:
1
2
3
4
5
6
7
8
enum
{
  bordered = 1,
  resizable = bordered << 1
};

// which allows ORing of the enumerator:
myWindowOptions = resizable | bordered;


They are not typedefs. It's more like you are creating a new type.
Last edited on
LB Wrote
Enums are deprecated, and you should not use them.


What should you use instead of enums?
Just a note, enum classes are also referred to as scoped enums, because the enum type shown by LowestOne does not follow scope rules intuitively.
closed account (Dy7SLyTq)
why are enums deprecated? they still have plenty of uses and make my data more organized. i dont need them, but it would make it a lot harder and a lot more code to use other things
Enum Classes, aka Scoped Enums, are not deprecated. Regular Enums are deprecated.
AFAIK, unscoped enumerations are not deprecated.
Ah, I guess I had just assumed they were. Oh well, I still don't like unscoped enums :)
I think an unscoped enum at namespace, class or function scope ought to be perfectly acceptable.

1
2
3
4
5
6
7
8
9
10
11
12
13
namespace calendar
{
      enum month_t { JAN = 0, FEB /* ... */ };
      // ...
}

struct book
{
      enum binding_t { PAPERBACK, HARDCOVER, SPIRAL, NONE };
      // ...
};

// etc.. 

But it still is implicitly convertible to an int and not its own type, which is why I don't like it.
closed account (Dy7SLyTq)
the point of an enum isnt to create a new type. its to help make data a bit easier to read, like with sfml video modes, instead of just doing ints. if you want to make a type use a data structure
Why are you arguing with my opinion?
But it still is implicitly convertible to an int and not its own type, which is why I don't like it.


However, when any integer is a valid argument, then this conversion is helpful:
1
2
3
4
5
6
7
8
9
10
11
12
struct Color
{
  enum {
    Red   = 0xFF0000,
    Green = 0x00FF00,
    Blue  = 0x0000FF
  };
};

// allows
function( Color::Red );
function( 0xFFFFFF );   // oops, forgot white 
You shouldn't use an enumeration for that, those should be constant variables. I'm really against the idea of assigning specific values to enumeration elements.
But it still is implicitly convertible to an int and not its own type, which is why I don't like it.


In my C code I would be doing something like this:

1
2
3
4
5
6
7
8
enum Colors
{
    Red,
    White,
    Blue
};

void pixel_draw(int x, int y, enum Colors c);


If another programmer willingly disregards the signature of that function, then the problems are much greater than anything a "proper" enum type could fix.
That's not the point; with scoped enums having their own type, you can do proper function overloading. I'd like to see that with regular enums.
Last edited on
closed account (Dy7SLyTq)
i feel like ive had this argument with fred before... its not the point of enums to supply that. its job is to make data a bit more clear, like typedef
I'm also not a fan of weak type aliases, I would prefer strong type aliases.
Pages: 12