Sign in to follow this  
xegoth

Enum question.

Recommended Posts

xegoth    154
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. :/

Share this post


Link to post
Share on other sites
Gor435    426
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.

Share this post


Link to post
Share on other sites
JohnBolton    1372
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;
...


Share this post


Link to post
Share on other sites
SiCrane    11839
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.

Share this post


Link to post
Share on other sites
guyaton    130
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

Share this post


Link to post
Share on other sites
Simian Man    1022
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

Share this post


Link to post
Share on other sites
Zahlman    1682
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this