enum value question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tino

    enum value question

    In the following code, can the indicated else statement ever be
    reached? It seems to me that if either Val1 or Val2 are not passed to
    the constructor, then there will be a compilation error. Am I
    correct?

    #include <iostream>
    using std::cout;
    using std::endl;

    typedef enum { Val1, Val2 } Venum;

    class A
    {
    int n;
    public:
    A( const Venum v )
    {
    if( v == Val1 )
    n = 1;
    else if( v == Val2 )
    n = 2;
    else // <---- Is this necessary
    n = 0;
    }

    int get() const
    {
    return n;
    }
    };

    int main()
    {
    A a1( Val1 ), a2( Val2 );
    cout << "a1: " << a1.get() << ", a2: " << a2.get() << endl;
    return 0;
    }
  • Sharad Kala

    #2
    Re: enum value question


    "Tino" <tino52@yahoo.c om> wrote in message
    news:f9d112e6.0 401272019.7400c 211@posting.goo gle.com...[color=blue]
    > In the following code, can the indicated else statement ever be
    > reached?[/color]

    No[color=blue]
    >It seems to me that if either Val1 or Val2 are not passed to
    > the constructor, then there will be a compilation error. Am I
    > correct?[/color]
    Yes...since there is no default constructor for class A.

    Best wishes,
    Sharad


    Comment

    • Daniel T.

      #3
      Re: enum value question

      In article <f9d112e6.04012 72019.7400c211@ posting.google. com>,
      tino52@yahoo.co m (Tino) wrote:
      [color=blue]
      > In the following code, can the indicated else statement ever be
      > reached? It seems to me that if either Val1 or Val2 are not passed to
      > the constructor, then there will be a compilation error. Am I
      > correct?
      >
      > #include <iostream>
      > using std::cout;
      > using std::endl;
      >
      > typedef enum { Val1, Val2 } Venum;
      >
      > class A
      > {
      > int n;
      > public:
      > A( const Venum v )
      > {
      > if( v == Val1 )
      > n = 1;
      > else if( v == Val2 )
      > n = 2;
      > else // <---- Is this necessary
      > n = 0;
      > }
      >
      > int get() const
      > {
      > return n;
      > }
      > };
      >
      > int main()
      > {
      > A a1( Val1 ), a2( Val2 );
      > cout << "a1: " << a1.get() << ", a2: " << a2.get() << endl;
      > return 0;
      > }[/color]

      Presumably, you have already tried to do it with your compiler yes? What
      happened when you did:

      int main()
      {
      A a1( (Venum)5 );
      cout << "a1: " << a1.get() << endl;
      }

      Comment

      • Jeff Schwab

        #4
        Re: enum value question

        Tino wrote:[color=blue]
        > In the following code, can the indicated else statement ever be
        > reached? It seems to me that if either Val1 or Val2 are not passed to
        > the constructor, then there will be a compilation error. Am I
        > correct?
        >
        > #include <iostream>
        > using std::cout;
        > using std::endl;
        >
        > typedef enum { Val1, Val2 } Venum;
        >
        > class A
        > {
        > int n;
        > public:
        > A( const Venum v )
        > {
        > if( v == Val1 )
        > n = 1;
        > else if( v == Val2 )
        > n = 2;
        > else // <---- Is this necessary
        > n = 0;
        > }
        >
        > int get() const
        > {
        > return n;
        > }
        > };
        >
        > int main()
        > {
        > A a1( Val1 ), a2( Val2 );
        > cout << "a1: " << a1.get() << ", a2: " << a2.get() << endl;
        > return 0;
        > }[/color]

        It depends. On some implementations , Venum will be able to hold more
        than one bit, and values other than Val1 and Val2 will be possible. On
        other implementations , this will not be the case. In general, be
        prepared for enum's to hold unintended values. For example: If you put
        three identifiers in your enum's definition, instead of two, you
        probably will be able to construct a Venum from the value 3, and an A
        from the resulting Venum.

        Comment

        • Howard

          #5
          Re: enum value question


          "Sharad Kala" <no.spam_sharad k_ind@yahoo.com > wrote in message
          news:bv7e3u$osi s3$1@ID-221354.news.uni-berlin.de...[color=blue]
          >
          > "Tino" <tino52@yahoo.c om> wrote in message
          > news:f9d112e6.0 401272019.7400c 211@posting.goo gle.com...[color=green]
          > > In the following code, can the indicated else statement ever be
          > > reached?[/color]
          >
          > No[color=green]
          > >It seems to me that if either Val1 or Val2 are not passed to
          > > the constructor, then there will be a compilation error. Am I
          > > correct?[/color]
          > Yes...since there is no default constructor for class A.
          >[/color]

          That's not neccessarily true. I'm not sure what the standard says about it,
          but in my tests I've been able to define a variable of type Venum, then
          assign a value to it via a c-style cast, and then create an object of class
          A passing it an invalid value. Like this:

          Venum e;
          e = (Venum)25;
          A testA(e);

          This calls that same constructor with the correct type, but at runtime the
          value is 25, which is neither Val1 or Val2.

          Perhaps it is implementation-dependant, but it works for me!

          I always provide a default case (using switch statements) when handling
          enumerations, (and may also add Assert statements) to make sure I don't have
          invalid values when passing enumerated values.

          -Howard



          Comment

          • Keith H Duggar

            #6
            Re: enum value question

            Daniel and Jeff are both correct.

            I'm not sure what your intent is with this enum so I have a few
            questions.

            First, since you are using enums why does A contain an int instead of
            a Venum? Second, does it make sense in your context to encapsulate the
            enum inside the class to prevent name pollution? If so, encapsulation
            will also all you to control access to the enum to preventing casting.

            In other words, how about this code ( names changed a bit ):

            #include <iostream>

            class Venomous {

            enum Venom { HAEMO , NEURO } ;

            Venom __venom ;

            public :

            static Venom const haemo ( ) { return HAEMO ; }
            static Venom const neuro ( ) { return NEURO ; }

            Venomous ( Venom venom ) : __venom ( venom ) { }

            Venom venom ( ) const { return __venom ; }

            void venom ( Venom venom ) { __venom = venom ; }

            } ;

            std::ostream & operator<< ( std::ostream & s , Venomous v ) {
            return s << (
            v.venom() == Venomous::haemo () ? "HAEMO\n" :
            v.venom() == Venomous::neuro () ? "NEURO\n" : "UNKNOWN\n" ) ;
            }

            int main ( int argc , char* argv[] ) {

            Venomous viper ( Venomous::haemo () ) ;
            Venomous cobra ( Venomous::neuro () ) ;

            // the following will not compile
            Venomous basilisk ( (Venomous::Veno m) 1000 ) ;

            std::cout << viper << cobra << basilisk ;

            }

            This solution seems to solve your problem. However, clients will not
            be able to allocate or manipulate objects of type Venom. If you want
            to allow this in addition to restricting values you will need to
            change Venom to a class with a private constructor and static
            functions that provide valid values similar to the above. This is
            described by Meyers in one of the "Effective" books using a Month
            class to illustrate.

            Comment

            • Sharad Kala

              #7
              Re: enum value question


              "Howard" <alicebt@hotmai l.com> wrote in message
              news:bv8l8r$479 @dispatch.conce ntric.net...[color=blue]
              >
              > "Sharad Kala" <no.spam_sharad k_ind@yahoo.com > wrote in message
              > news:bv7e3u$osi s3$1@ID-221354.news.uni-berlin.de...[color=green]
              > >
              > > "Tino" <tino52@yahoo.c om> wrote in message
              > > news:f9d112e6.0 401272019.7400c 211@posting.goo gle.com...[color=darkred]
              > > > In the following code, can the indicated else statement ever be
              > > > reached?[/color]
              > >
              > > No[color=darkred]
              > > >It seems to me that if either Val1 or Val2 are not passed to
              > > > the constructor, then there will be a compilation error. Am I
              > > > correct?[/color]
              > > Yes...since there is no default constructor for class A.
              > >[/color]
              >
              > That's not neccessarily true. I'm not sure what the standard says about it,
              > but in my tests I've been able to define a variable of type Venum, then
              > assign a value to it via a c-style cast, and then create an object of class
              > A passing it an invalid value. Like this:
              >
              > Venum e;
              > e = (Venum)25;
              > A testA(e);
              >
              > This calls that same constructor with the correct type, but at runtime the
              > value is 25, which is neither Val1 or Val2.
              >
              > Perhaps it is implementation-dependant, but it works for me!
              >
              > I always provide a default case (using switch statements) when handling
              > enumerations, (and may also add Assert statements) to make sure I don't have
              > invalid values when passing enumerated values.[/color]

              True, even I have observed this.
              I was answering in the context that if no value is passed to the c'tor then what
              would be the behavior.
              Thanks for pointing that out.

              -Sharad


              Comment

              • Nick Hounsome

                #8
                Re: enum value question


                "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                news:2N6dnYD8EJ _8F4rd4p2dnA@co mcast.com...[color=blue]
                > Tino wrote:[color=green]
                > > In the following code, can the indicated else statement ever be
                > > reached? It seems to me that if either Val1 or Val2 are not passed to
                > > the constructor, then there will be a compilation error. Am I
                > > correct?
                > >
                > > #include <iostream>
                > > using std::cout;
                > > using std::endl;
                > >
                > > typedef enum { Val1, Val2 } Venum;[/color][/color]

                Why not
                enum Venum {Val1,Val2}; ??

                Better still why not Venom instead of Venum?
                [color=blue][color=green]
                > >
                > > class A
                > > {
                > > int n;
                > > public:
                > > A( const Venum v )
                > > {
                > > if( v == Val1 )
                > > n = 1;
                > > else if( v == Val2 )
                > > n = 2;
                > > else // <---- Is this necessary[/color][/color]

                Not unless you want to try to defend against client code
                deliberately being perverse.
                [color=blue][color=green]
                > > n = 0;[/color][/color]

                How does this help - If you are going to try to continue I
                would expect you to use one of the 'valid' values.
                [color=blue][color=green]
                > > }
                > >
                > > int get() const
                > > {
                > > return n;
                > > }
                > > };
                > >
                > > int main()
                > > {
                > > A a1( Val1 ), a2( Val2 );
                > > cout << "a1: " << a1.get() << ", a2: " << a2.get() << endl;
                > > return 0;
                > > }[/color]
                >[/color]

                Why not just
                enum Venum {Val1=1,Val2=2} ;
                and forget about A?
                [color=blue]
                > It depends. On some implementations , Venum will be able to hold more
                > than one bit, and values other than Val1 and Val2 will be possible. On
                > other implementations , this will not be the case. In general, be
                > prepared for enum's to hold unintended values. For example: If you put
                > three identifiers in your enum's definition, instead of two, you
                > probably will be able to construct a Venum from the value 3, and an A
                > from the resulting Venum.
                >[/color]

                I may be wrong but I think the standard might actually say that it will
                be the same underlying thing as int unless it needs to be long because of
                one of the declared values.


                Comment

                Working...