Clarification of enumeration

  • Thread starter Caroline Middlebrook
  • Start date
C

Caroline Middlebrook

Hi,

I have been reading through the Stroustrup book 3e (section 4.8) 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.
 
J

JKop

Caroline Middlebrook posted:
Hi,

I have been reading through the Stroustrup book 3e (section 4.8) 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
 
M

Michael Kurz

Caroline Middlebrook said:
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
 
M

Michael Kurz

JKop said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top