Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Clarification of enumeration

Reply
Thread Tools

Clarification of enumeration

 
 
Caroline Middlebrook
Guest
Posts: n/a
 
      10-15-2004
Hi,

I have been reading through the Stroustrup book 3e (section 4. about
enumerations and there is something I just don't understand...

I thought that the idea of an enum was that you could define some type
that had a set number of values defined by you eg

enum days_of_week {mon, tue, wed, thu, fri, sat, sun};

I was under the impression that if you define a variable of type
days_of_week that it could *only* be one of those seven values listed
above. I typed in the code example in section 4.8 and tested it out
and it seems that you can declare a variable of that type and assign
to it any integer that falls within the range of the enum but that
that integer may not equate to one of the values you defined for the
enum.

So, if I understand correctly, the range for the above will be 0:31?
meaning that I could assign any integer from 0-31 to a days_of_week
variable. In which case lets say I assigned say 19, and then did a
switch on the values above, it would drop down the the default case.

So, whats the point of the enum if values are not restricted to the
ones you specify?

Many thanks,
Caroline M.
 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      10-15-2004
Caroline Middlebrook posted:

> Hi,
>
> I have been reading through the Stroustrup book 3e (section 4. about
> enumerations and there is something I just don't understand...
>
> I thought that the idea of an enum was that you could define some type
> that had a set number of values defined by you eg
>
> enum days_of_week {mon, tue, wed, thu, fri, sat, sun};
>
> I was under the impression that if you define a variable of type
> days_of_week that it could *only* be one of those seven values listed
> above. I typed in the code example in section 4.8 and tested it out
> and it seems that you can declare a variable of that type and assign
> to it any integer that falls within the range of the enum but that
> that integer may not equate to one of the values you defined for the
> enum.
>
> So, if I understand correctly, the range for the above will be 0:31?
> meaning that I could assign any integer from 0-31 to a days_of_week
> variable. In which case lets say I assigned say 19, and then did a
> switch on the values above, it would drop down the the default case.
>
> So, whats the point of the enum if values are not restricted to the
> ones you specify?
>
> Many thanks,
> Caroline M.



Exactly why I don't bother with them.

I myself use global const variables instead.

If I *really* want to limit values, I write a class that has a private
function called "Set"; when the initializer or assignment operator is
called, they in turn call "Set", which validates the input. If it's bad,
then throw bad_data();


-JKop
 
Reply With Quote
 
 
 
 
Michael Kurz
Guest
Posts: n/a
 
      10-15-2004

"Caroline Middlebrook" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) om...
>
> enum days_of_week {mon, tue, wed, thu, fri, sat, sun};
>
> I was under the impression that if you define a variable of type
> days_of_week that it could *only* be one of those seven values listed
> above. I typed in the code example in section 4.8 and tested it out
> and it seems that you can declare a variable of that type and assign
> to it any integer that falls within the range of the enum but that
> that integer may not equate to one of the values you defined for the
> enum.
>
> So, if I understand correctly, the range for the above will be 0:31?
> meaning that I could assign any integer from 0-31 to a days_of_week
> variable. In which case lets say I assigned say 19, and then did a
> switch on the values above, it would drop down the the default case.
>


Strange, all C++ compilers I tested do not accept this, without casting.
the following code gives an error on VC7, gcc 3.3.2 and comeau online:

<snip>
enum myEnum{ red, yellow, kk};

int main(int argc, char* argv[]){
myEnum s;
s = red; // ok
s = 0; // error
}
</snip>

So either I missed your point or does your compiler accept the above code?

Even the example in my version of the Stroustrup C++, shows that or do you
complain that the compiler
allows an explict conversion from an int type to an enumeration type like:

"s = myEnum(99)" without causing an compiler error?

BTW: my VC7 does not like this either, it forces a cast (myEnum)99, but I
fear this feature is not std compliant.


IMHO this is C++ style as well as it was C style.
You can tell the compiler many things, which are then ok from its syntactial
point of view, but with a questionable semantic.



Regards
Michael


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      10-15-2004

> enum myEnum{ red, yellow, kk};
>
> int main(int argc, char* argv[]){
> myEnum s;
> s = red; // ok
> s = 0; // error
> }



Try:

s = myEnum(0);


-JKop
 
Reply With Quote
 
Michael Kurz
Guest
Posts: n/a
 
      10-15-2004

"JKop" <(E-Mail Removed)> schrieb im Newsbeitrag
news:tZNbd.37087$(E-Mail Removed)...
>
> > enum myEnum{ red, yellow, kk};
> >
> > int main(int argc, char* argv[]){
> > myEnum s;
> > s = red; // ok
> > s = 0; // error
> > }

>
>
> Try:
>
> s = myEnum(0);
>


I already have, as I wrote in my previous post. But this is an explicit
conversion, so I dont see a problem.
Specially as
<snip>
class CTest{};
int main(){
char buf[256];
CTest *tst = (CTest*)buf;
}

</snip>

will compile as well. So if the programmer has to "explicit"ly tell the
compiler how things should run, from my
point of view this is as ok as it is C++ style.

Regards
Michael


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Filling a listbox with the values in a enumeration Timothy Parez ASP .Net 3 06-29-2010 05:59 AM
Re-using a simple type definition; with enumeration constraint andwithout enumeration constraint puvit82 XML 4 02-01-2008 03:46 PM
enumeration types Charlie VHDL 4 02-09-2005 03:01 PM
Visibility of enumeration literals under use clauses biau@altavista.com VHDL 15 02-01-2005 04:19 AM
Record, Enumeration & std_logic_vector Jan Kindt VHDL 5 09-12-2003 10:25 AM



Advertisments