|
|
|
|
![]() ![]() |
Feb 20 2005, 08:54 PM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 407 Joined: 13-December 04 Member No.: 2,696 |
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 |
|
|
|
Feb 20 2005, 09:26 PM
Post
#2
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 144 Joined: 19-February 05 From: Nakorn Chaisri, Thailand Member No.: 3,864 |
QUOTE(kvarnerexpress @ Feb 21 2005, 03:54 AM) 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 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://cplus.about.com/od/learnin2/l/aa082104e.htm 3. http://www.cplusplus.com/doc/tutorial/tut3-6.html 4. http://cplus.about.com/od/beginnerctutorial/l/aa021702a.htm Hope this helps |
|
|
|
Feb 21 2005, 12:45 AM
Post
#3
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 399 Joined: 14-November 04 From: Elysium Member No.: 2,280 |
I think it's only defined in a hash format (day="monday")
but can be made for an index format. Last time I was at odds with the issue, it changed from standard to standard even across compilers. Even we sages do not know now ... so tell us what you find. |
|
|
|
Feb 21 2005, 10:47 AM
Post
#4
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 142 Joined: 24-December 04 From: Queensland, Australia Member No.: 2,902 |
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... CODE 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. |
|
|
|
Apr 14 2005, 03:57 PM
Post
#5
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 937 Joined: 14-April 05 From: West Chester, PA Member No.: 5,636 |
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; |
|
|
|
![]() ![]() |
Similar Topics
| Topics | Topics | ||||||
|---|---|---|---|---|---|---|---|
|
| ||||||
|
Lo-Fi Version | Time is now: 7th October 2008 - 09:19 PM |