473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Clarification of enumeration

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.
Jul 22 '05 #1
4 1240
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
Jul 22 '05 #2

"Caroline Middlebrook" <cm**********@btinternet.com> schrieb im Newsbeitrag
news:12**************************@posting.google.c 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
Jul 22 '05 #3
enum myEnum{ red, yellow, kk};

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

Try:

s = myEnum(0);
-JKop
Jul 22 '05 #4

"JKop" <NU**@NULL.NULL> schrieb im Newsbeitrag
news:tZ*******************@news.indigo.ie...
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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Justin Wright | last post by:
I know that I can set up an enumeration as follows ( just typed in quick so may have syntax errors ): <xsd:simpleType name="colors"> <xsd:restriction base="xsd:string"> <xsd:enumeration...
8
by: aevans1108 | last post by:
Greetings I can't seem to inherit enumerated values from a globally defined type in my XML schema. XmlSchema.Compile() doesn't like it. Here's the schema. <?xml version="1.0"...
2
by: Mark | last post by:
Assume you have an enumeration like PhoneType { Home, Business, Cell }. This enumeration corresponds with a lookup/dictionary table in your database like: phone_cd | phone_descr 1 ...
4
by: Marshal | last post by:
Sure... IEnumerable was inconvenient suggesting a separate class to service the enumeration, IEnumerator, and multiple operations: Current, MoveNext, Reset. (I'll warp the definition of "operation"...
1
by: Stefano G. | last post by:
I have a WSDL containing this enumeration type <xsd:simpleType name="item_type_enum"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="VCD"/> <xsd:enumeration value="SVCD"/>...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
3
by: Davidoff | last post by:
Hi, I parse an XML file with a XSD schema. One XmlNode has an attribute whose type is a restriction of xs:string : <xs:simpleType name="stypeDay"> <xs:restriction base="xs:string">...
0
by: news.emn.fr | last post by:
Hello, i got this attribute <xs:attribute name="jour"> <xs:simpleType> <xs:restriction base="stypeJour"> </xs:restriction> </xs:simpleType> </xs:attribute>
3
by: eXt | last post by:
For a realtime-application (half an application at least, kind of a framework) I am working with I need a event managing system and naturally it must be fast. I have defined a set of available...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.