You are here:

C++/enumuration values

Advertisement


Question
Hi
could you plz tell me that are enumuration values coerced to integer?or are any other types coerced to an enumuration type?

Thanx
Bita

Answer
An enumerated type is used to express the idea that a variable will be used for a specific purpose and should only be able to have a small limited number of values - for instance, a variable that stores the current direction only needs to store four values corresponding to north, south, east, and west. We can use an enumerated type for this:

enum direction_type { NORTH, EAST, SOUTH, WEST } ;


direction_type is a user-defined (enumerated) type and we can now use variables of this type. These variables can only hold one of the "literal" values NORTH, EAST, SOUTH or WEST. for example:

direction_type wind_direction = EAST ;

There is an implicit conversion from an enumerated type to an integral value; C++ allows the programmer to specify the intehgral value explicitly. for example,

enum colour_type { BLACK=0, RED=1, GREEN=2, BLUE=4, WHITE = RED|GREEN|BLUE } ;

If no value is specified, the first enum literal takes 0, and every other enum literal takes a value one greater than the prvious. for example,

enum state_type { IDLE, BUSY=4, UNKNOWN } ;

Here, IDLE would be converted to the integral value 0, UNKNOWN to 5 (4+1).

int i = wind_direction ; // i initialised with 1 (EAST)

std::cout << BLUE ; // prints out 4

if( BUSY > wind_direction ) // both BUSY and wind_direction are converted implicitly
                           // to integral values, and these are then compared.

There is no implicit conversion from an integral type (or any other standard type) to an enumerated type. for example,
                                         
wind_direction = 3 ; // error, no conversion from int to direction_type

wind_direction = direction_type(3) ; // ok, convert via an explicit cast
                                    // wind_direction is now WEST

wind_direction = direction_type(25) ;

will compile, but the results are undefined. 25 is not within the range of the enum.

In short, an value of enumerated type can be implicitly converted to an integral type.
An integral value can not be implicitly converted to a value of enumerated type; a cast is required and the correctness of the cast is the responsibility of the programmer.

An unnamed (or anonymous) enum is just a shortcut for defining literal integral constants. for example,

enum { FIVE=5, EIGHT=8, DOZEN=12 } ;

is equivalent to

const int FIVE = 5 ;
const int EIGHT = 8 ;
const int DOZEN = 12 ;  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++09. i would not answer questions about gui and web programming.

Experience

over 15 years

Education/Credentials
post graduate engineer

©2012 About.com, a part of The New York Times Company. All rights reserved.