[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  General
. . -> [Subject]  C++ enumeration type
Home  |  Users  |  Search  |  FAQ
Register forum user name
Forgotten password?

C++ enumeration type

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Posted by Nick Cash   USA  (626 posts)  [Biography] bio
Date Thu 21 Dec 2006 02:00 AM (UTC)

Amended on Thu 21 Dec 2006 02:02 AM (UTC) by Nick Cash

Hello again everyone!

While working on a small project I decided to use C++'s enumeration type, which looks like this:

enum EState { eSamsara, eMind, eBody, eSoul, eDamian, eNirvana };

I've consulted a few documents briefly on using it within loops, and as far as I can see its only a pain in the neck. There are no attributes like Ada, and you can't just add one like you could in C. I want to do a loop like this:

for ( EState state = eSamsara; state < eNirvana; state++ )
  // code here

However, the only way I've been able to get it to work is to have a switch to handle each case to update state for the next iteration or to break if need be, which is a huge pain in the neck. Should I scrap the enum data type for another or am I missing something?

Thanks. :)

~Nick Cash
[Go to top] top

Posted by Zeno   USA  (2,871 posts)  [Biography] bio
Date Reply #1 on Thu 21 Dec 2006 02:19 AM (UTC)
You should be able to do that loop. I've done it before, are you getting an error message?

Oh yeah, now I remember. You need to make it an int, then cast it. I think.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
[Go to top] top

Posted by Nick Cash   USA  (626 posts)  [Biography] bio
Date Reply #2 on Thu 21 Dec 2006 03:28 AM (UTC)

Amended on Thu 21 Dec 2006 03:32 AM (UTC) by Nick Cash

Not a very good looking solution, but it does work as noted below:

for ( int state = (int)eSamsara; state < (int)eNirvana; state++ )
  // code here

Casting all of the constants is quite a pain, but at least it works. Were this not a short term project I'd look for a more elegant solution.

Interestingly enough, within the loop I can cast state to EState with no dreary side effects, saving me quite a few casts. I still dislike such dirty casting. Always good to learn something new though.

Thanks Zeno.

~Nick Cash
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #3 on Wed 03 Jan 2007 01:42 PM (UTC)

Amended on Wed 03 Jan 2007 01:52 PM (UTC) by David Haley

This actually makes sense if you think of enumerations as not being sequences but as a special data type that can take one of several values. Being able to increment over them is a left-over from C's implementation of enumerations as integers. But really, an enumeration shouldn't be thought of as a series of numbers; that's why it forces you to cast them. (It is a bit clumsy, of course, when you want to iterate over all values of an enum, without caring what order you get them in. Java lets you do that quite nicely.)

P.S. I just got back from a vacation, hence my absence the past two weeks. :)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum

[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]