Clarification of enumeration

Discussion in 'C++' started by Caroline Middlebrook, Oct 15, 2004.

  1. 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.
     
    Caroline Middlebrook, Oct 15, 2004
    #1
    1. Advertising

  2. Caroline Middlebrook

    JKop Guest

    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
     
    JKop, Oct 15, 2004
    #2
    1. Advertising

  3. Caroline Middlebrook

    Michael Kurz Guest

    "Caroline Middlebrook" <> schrieb im Newsbeitrag
    news:...
    >
    > 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
     
    Michael Kurz, Oct 15, 2004
    #3
  4. Caroline Middlebrook

    JKop Guest


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



    Try:

    s = myEnum(0);


    -JKop
     
    JKop, Oct 15, 2004
    #4
  5. Caroline Middlebrook

    Michael Kurz Guest

    "JKop" <> schrieb im Newsbeitrag
    news:tZNbd.37087$...
    >
    > > 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
     
    Michael Kurz, Oct 15, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timothy Parez
    Replies:
    3
    Views:
    3,787
    WhatIThink
    Jun 29, 2010
  2. Jan Kindt
    Replies:
    5
    Views:
    2,086
    Allan Herriman
    Sep 12, 2003
  3. Replies:
    15
    Views:
    1,346
    Jerry Coffin
    Feb 1, 2005
  4. Charlie

    enumeration types

    Charlie, Feb 8, 2005, in forum: VHDL
    Replies:
    4
    Views:
    571
    Charlie
    Feb 9, 2005
  5. puvit82
    Replies:
    4
    Views:
    769
    puvit82
    Feb 1, 2008
Loading...

Share This Page