What exactly does 'enum' do?

Started by
8 comments, last by Daerus 21 years, 1 month ago
Hello, I''m pretty new to C/C++ and I was just wondering if someone would be able to explain to me what enum does, exactly, and possibly give me an example. I tried using the search, but it just brings up an error.. Any help is appreciated, thanks.
Advertisement
enum = enumerate

eg

enum Colour {RED, GREEN, BLUE};Colour c = BLUE; 


or

enum {SUCCESS, FAILURE};int iReturn = SUCCESS; 


[edited by - Will O on March 12, 2003 5:14:54 PM]
It's a way of defining some constants sequentially without having to worry about duplicating any of the values.

enum defActionType{   ACTION_NONE = 0,   ACTION_WALK,   ACTION_RUN,   ACTION_DUCK,   ACTION_FIRE,   ACTION_QTY};defActionType action;  


The resulting values are:

ACTION_NONE = 0
ACTION_WALK = 1
ACTION_RUN = 2
ACTION_DUCK = 3
ACTION_FIRE = 4
ACTION_QTY = 5

The action variable will only accept the enumeration variables (action = ACTION_DUCK; )

I like to initialize the first one to zero. That way my last enum, with the _QTY, holds the quantity that can be used to make an array in case I want to base the elements of that array off from each enum of a given group.

[edited by - Waverider on March 12, 2003 5:20:12 PM]
It's not what you're taught, it's what you learn.
It starts on 0 if you do not specify anything else, it''s a good idea to do so though
so why not use #define? what are the differences?
pro''s vs. con''s and which should be used in which context?
quote:Original post by Anonymous Poster
so why not use #define? what are the differences?
pro''s vs. con''s and which should be used in which context?

enums are type safe, defines are not.


Update GameDev.net system time campaign - success at last
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
More importantly, enum''s following scoping rules. For example, you could define two classes, each with different definitons of the same enum identifier and use them in harmony. Whereas if you #define''d the same identifier to two different values, bad things could happen.
quote:Original post by Anonymous Poster
so why not use #define? what are the differences?
That one uses textual substitution and one doesn''t?
It also enforces a limited list of values for that variable.

You can only assign as a value, one of the values you''ve enumerated.

- Ben Scott
Just starting out
- Ben Scott
Enums can also make very readable code when used as a function parameter for example:


   enum eLineStyle {	Solid,	Dot,	Dot-Dash};// Function declaration void DrawHLine(int startX, int endX, eLineStyle LineApperence);// Use the functionDrawLine(5, 50, Solid);  


#define is an instruction to the preprocessor where as enum and const are actually part of the C++ language. This is simular to using preprocessor macros and C++ inline functions. Using the standard language keywords such is const and inline is much safer than #define.

This topic is closed to new replies.

Advertisement