Jump to content
xisto Community
kvarnerexpress

Enums

Recommended Posts

if i'm using enums, i can use it as the variable name and as a numerical value, correct?

ex:

enum dayOfWeek {monday, tuesday, wednesday};

dayOfWeek day;

 

day=monday;

or

day=1;

 

or it can only be used one way?

please help

52409[/snapback]

I believe you refer to the enum list using the variables that you have specified inside.. I'm not sure whether you can refer to it using the index number.. i.e. i've always used it in the form: day = monday, though I'd say there's no reason why you shouldn't be able to use an integer instead of the variable name... If you want more details on enum, follow these links:

 

1. http://www.functionx.com/cpp/Lesson07.htm

2. http://forums.xisto.com/no_longer_exists/

3. http://forums.xisto.com/no_longer_exists/

4. http://forums.xisto.com/no_longer_exists/

 

Hope this helps ;)

Share this post


Link to post
Share on other sites

I think it's only defined in a hash format (day="monday")but can be made for an index format. Last time I wasat odds with the issue, it changed from standard to standardeven across compilers. Even we sages do not know now ;)... so tell us what you find.

Share this post


Link to post
Share on other sites

Well since dayOfWeek is now a new type, to be able to make day = 1 work, you actually have to use,

day = (dayOfWeek)1;

The question that begs to be asked is "Why?". Enums are there to make your life easier, in both writing and maintaining the code (meaning you don't have to remember exact values, just names... much easier.

But basically, they are just like declaring some constants and using them with an integer...

const monday = 1;const tuesday = 2;const wednesday = 3;int main(){  int day = monday;  if(day == monday)  {        ....

You get the picture... using enums is just a simpler way of doing that.

Also, I might mention that enums start at 0 usually by default, but you can change that by declaring... (in this case), enum dayOfWeek {monday = 1, tuesday, wednesday };


Hope that answers your question.

Share this post


Link to post
Share on other sites

It would work both ways but for one it is normally common practice to make all enums capital because they act as constants. Ex. enum daysofWeek {MONDAY};Secondly, you can not use it like today = 0; you would have to cast the 1 to a daysofWeek like today = (daysofWeek)0;

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

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.