What exactly does 'enum' do?
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.
enum = enumerate
eg
or
[edited by - Will O on March 12, 2003 5:14:54 PM]
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.
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]
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]
so why not use #define? what are the differences?
pro''s vs. con''s and which should be used in which context?
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?
enum
s are type safe, define
s are not. Update GameDev.net system time campaign - success at last
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 PosterThat one uses textual substitution and one doesn''t?
so why not use #define? what are the differences?
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
You can only assign as a value, one of the values you''ve enumerated.
- Ben Scott
Just starting out
Enums can also make very readable code when used as a function parameter for example:
#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.
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
Popular Topics
Advertisement