Enum question.

Started by
8 comments, last by xegoth 18 years, 8 months ago
What's the difference between:

const enum PlacesILike
{
	Paris,
	SanDiego,
	Hawaii
}

and

enum PlacesILike
{
	Paris,
	SanDiego,
	Hawaii
}

I forget what const means in that context. :/
Advertisement
IIRC, the first one is invalid.
Quote:IIRC, the first one is invalid.

Even if it weren't They would be the same. An enumeration is just a list of constants.
F-R-E-D F-R-E-D-B-U-R...G-E-R! - Yes!
Quote:Original post by xegoth
What's the difference between:

*** Source Snippet Removed ***
and

*** Source Snippet Removed ***

I forget what const means in that context. :/


const means the information is constant it cannot be overwritten.

Example:

const char title[] = "Title of My Game";

since title has been declared as const or constant the value cannot be changed thoughout the execution of the program.
Gor435 - My Journal - MySpace - Facebook
Quote:Original post by DigiDude
... An enumeration is just a list of constants.
Not quite, an enumeration is a type. The "list of constants" are the possible values of the type. The fact that an enumeration value can be converted to an integer value is handy, but it is kind of a hack. Here is an enum in action
    enum PlacesILike    {	Paris,	SanDiego,	Hawaii    };    char const * PlaceName( PlacesILike place )    {        char const * name;        switch ( place )        {            case Paris:    name = "Paris";     break;            case SanDiego: name = "San Diego"; break;            case Hawaii:   name = "Hawaii";    break;        }        return name;    }            ...        PlacesILike const   favoritePlace = Hawaii;        cout << "My favorite place is " << PlaceName( favoritePlace ) << endl;        ... 


John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Occassionally you'll see something like:
const enum PlacesILike{	Paris,	SanDiego,	Hawaii} place;

This statement does two things: it declares an enum type called PlacesILike and it declares a variable called places of type const PlacesILike. The const modifies the variable, not the type.
kind of off topic, but say you have:

enum PlacesILike
{
Paris,
SanDiego,
Hawaii
};

and say you have a function

void Function( PlacesILike place );

is PlacesILike converted to an int value for this? or is it unsigned int?

what if i do this:

void AnotherFunction( short MorePlaces );
AnotherFunction( Paris );

in the function is MorePlaces converted to a short when passed into the funtion? (inferign i should do: AnotherFunction( static_cast<short>( Paris ) ); )???

~guyaton
~guyaton
Quote:Original post by guyaton
enum PlacesILike
{
Paris,
SanDiego,
Hawaii
};

and say you have a function

void Function( PlacesILike place );

is PlacesILike converted to an int value for this? or is it unsigned int?


It will probably just be an int - though it does not have to be "converted" It just is...

Quote:Original post by guyaton
void AnotherFunction( short MorePlaces );
AnotherFunction( Paris );
~guyaton


I think it's a reinterpret_cast actually
Quote:Original post by guyaton
kind of off topic, but say you have:

enum PlacesILike
{
Paris,
SanDiego,
Hawaii
};

and say you have a function

void Function( PlacesILike place );

is PlacesILike converted to an int value for this? or is it unsigned int?


It is of type PlacesILike. It will probably be represented internally with an int, but in C++ (not in C, where the type system is considerably weaker) it is a separate type.

Quote:
void AnotherFunction( short MorePlaces );
AnotherFunction( Paris );

in the function is MorePlaces converted to a short when passed into the funtion?


Yes; a function can only receive the data type that it claims to accept (and if no conversion is possible, it will not compile).

Quote:(inferign i should do: AnotherFunction( static_cast<short>( Paris ) ); )???


Implying, not inferring (consult your dictionary); and no, the conversion is implicit so you needn't do anything.

However, in C++ (again not in C, where the type system is considerably weaker) a cast IS required to go in the other direction. E.g. PlacesILike x; x = static_cast<PlacesILike>(2); or more simply x = PlacesILike(2);.

Reference
Quote:Original post by Roboguy
IIRC, the first one is invalid.


It compiles just fine for me. Does it just have no effect?

This topic is closed to new replies.

Advertisement