default values to enum ?

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

    default values to enum ?

    hi there,

    i was looking for some way to give default values to enum, when ever i m
    creating an enum variable it shuld be INITIALIZED to certain default values
    is it possible ? or do i have to write some function for assigning values to
    them and call them myself ??


  • Sharad Kala

    #2
    Re: default values to enum ?


    "dumboo" <vthe2@yahoo.co m> wrote in message
    news:c19ges$1fi eab$1@ID-211285.news.uni-berlin.de...[color=blue]
    > hi there,
    >
    > i was looking for some way to give default values to enum, when ever i m
    > creating an enum variable it shuld be INITIALIZED to certain default values
    > is it possible ? or do i have to write some function for assigning values to
    > them and call them myself ??[/color]

    Enum values do have defaults.
    Consider enum Color {red, white, blue};
    In this case red will be 0, white will be 1 and blue will be 2.

    For more -


    Best wishes,
    Sharad




    Comment

    • Jonathan Turkanis

      #3
      Re: default values to enum ?


      "Sharad Kala" <no.spam_sharad k_ind@yahoo.com > wrote in message
      news:c19hfo$1fi ru0$1@ID-221354.news.uni-berlin.de...[color=blue]
      >
      > "dumboo" <vthe2@yahoo.co m> wrote in message
      > news:c19ges$1fi eab$1@ID-211285.news.uni-berlin.de...[color=green]
      > > hi there,
      > >
      > > i was looking for some way to give default values to enum, when[/color][/color]
      ever i m[color=blue][color=green]
      > > creating an enum variable it shuld be INITIALIZED to certain[/color][/color]
      default values[color=blue][color=green]
      > > is it possible ? or do i have to write some function for assigning[/color][/color]
      values to[color=blue][color=green]
      > > them and call them myself ??[/color]
      >
      > Enum values do have defaults.
      > Consider enum Color {red, white, blue};
      > In this case red will be 0, white will be 1 and blue will be 2.[/color]

      How does this establish defaults?

      Consider:

      #include <iostream>

      enum Color {red, white, blue};

      int main()
      {
      Color c;
      std::cout << "default Color = " << c <<" \n";
      }

      Try this on a couple of compilers.

      Jonathan


      Comment

      • Sharad Kala

        #4
        Re: default values to enum ?


        "Jonathan Turkanis" <technews@kanga roologic.com> wrote in message
        news:c19ic8$1fl 5b8$1@ID-216073.news.uni-berlin.de...[color=blue]
        >
        > "Sharad Kala" <no.spam_sharad k_ind@yahoo.com > wrote in message
        > news:c19hfo$1fi ru0$1@ID-221354.news.uni-berlin.de...[color=green]
        > >
        > > "dumboo" <vthe2@yahoo.co m> wrote in message
        > > news:c19ges$1fi eab$1@ID-211285.news.uni-berlin.de...[color=darkred]
        > > > hi there,
        > > >
        > > > i was looking for some way to give default values to enum, when[/color][/color]
        > ever i m[color=green][color=darkred]
        > > > creating an enum variable it shuld be INITIALIZED to certain[/color][/color]
        > default values[color=green][color=darkred]
        > > > is it possible ? or do i have to write some function for assigning[/color][/color]
        > values to[color=green][color=darkred]
        > > > them and call them myself ??[/color]
        > >
        > > Enum values do have defaults.
        > > Consider enum Color {red, white, blue};
        > > In this case red will be 0, white will be 1 and blue will be 2.[/color]
        >
        > How does this establish defaults?
        >
        > Consider:
        >
        > #include <iostream>
        >
        > enum Color {red, white, blue};
        >
        > int main()
        > {
        > Color c;
        > std::cout << "default Color = " << c <<" \n";
        > }
        >[/color]
        Took the question wrong..my bad.


        Comment

        • Sharad Kala

          #5
          Re: default values to enum ?


          "dumboo" <vthe2@yahoo.co m> wrote in message
          news:c19ges$1fi eab$1@ID-211285.news.uni-berlin.de...[color=blue]
          > hi there,
          >
          > i was looking for some way to give default values to enum, when ever i m
          > creating an enum variable it shuld be INITIALIZED to certain default values
          > is it possible ? or do i have to write some function for assigning values to
          > them and call them myself ??[/color]

          I don't know of a way to default initialize an enum variable.
          Probably someone could tell how to achieve what he has asked for.

          If the enum is part of a class then probably this could be a solution -

          #include <iostream>
          enum Color {red = 34, white = 42, blue = 55 };

          struct A{
          Color c;
          A():c(white){
          }
          A(Color C):c(C){}
          };

          int main()
          {
          A a;
          std::cout << "default Color = " << a.c <<" \n";
          A b(blue);
          std::cout << "Color = " << b.c <<" \n";
          }




          Comment

          • dumboo

            #6
            Re: default values to enum ?

            hi there,
            "Sharad Kala" <no.spam_sharad k_ind@yahoo.com > wrote in message
            news:c19nfn$1el udo$1@ID-221354.news.uni-berlin.de...
            [...]

            actually i m having an Array of enums...


            Comment

            • Max M.

              #7
              Re: default values to enum ?

              dumboo wrote:
              [color=blue]
              > hi there,
              >
              > i was looking for some way to give default values to enum, when ever i m
              > creating an enum variable it shuld be INITIALIZED to certain default
              > values is it possible ? or do i have to write some function for assigning
              > values to them and call them myself ??[/color]


              enum Foobars
              {
              John, Mary
              };


              template< typename EnumT, EnumT DefaultV >
              class Enum
              {
              EnumT value;
              public:
              Enum( EnumT v = DefaultV ) : value(v) { }
              operator EnumT() const { return value; }
              };

              int main()
              {
              Foobars v = John;
              Enum<Foobars,Ma ry> ev;
              ev = v;
              v = ev;
              if( ev == v ) { }
              if( ev == John ) { }
              switch( ev )
              {
              case Mary:
              case John: ;
              }
              }

              Max



              Comment

              • Sharad Kala

                #8
                Re: default values to enum ?


                "dumboo" <vthe2@yahoo.co m> wrote in message
                news:c19nnm$1g0 6e8$1@ID-211285.news.uni-berlin.de...[color=blue]
                > hi there,
                > "Sharad Kala" <no.spam_sharad k_ind@yahoo.com > wrote in message
                > news:c19nfn$1el udo$1@ID-221354.news.uni-berlin.de...
                > [...]
                >
                > actually i m having an Array of enums...[/color]

                Does this help?

                #include <iostream>
                enum Color {red = 34, white = 42, blue = 55 };
                struct Enum{
                Color c;
                Enum(Color c):c(c){}
                Enum():c(white) {}
                Enum& operator=(Color col)
                {
                this->c = col;
                return *this;
                }
                operator Color(){
                return c;
                }
                };

                int main(){
                Enum c[10];
                c[7] = blue;
                c[5] = red;
                for (int i=0; i<10; i++)
                std::cout << c[i] << "\n";
                }

                Output -
                42
                42
                42
                42
                42
                34
                42
                55
                42
                42

                Best wishes,
                Sharad


                Comment

                • torhu

                  #9
                  Re: default values to enum ?

                  Since you have the conversion constructor Enum(Color c), you don't really need the
                  operator=() either. :)

                  Comment

                  • Sharad Kala

                    #10
                    Re: default values to enum ?


                    "torhu" <thusaboe@hotma il.com> wrote in message
                    news:25b320.040 2221313.56b27a2 c@posting.googl e.com...[color=blue]
                    > Since you have the conversion constructor Enum(Color c), you don't really need[/color]
                    the[color=blue]
                    > operator=() either. :)[/color]

                    True, I would expect ideally two invocations, namely constructor and
                    copy-constructor in this case.
                    With assignment operator I would expect just one call.
                    I know most compilers do elide the copy-constructor part.
                    If I make the copy constructor private I should expect the program to break,
                    provided no assignment operator is defined.

                    Section 12.8/14
                    "A program is ill-formed if the copy constructor or the copy assignment operator
                    for an object is implicitly used and the
                    special member function is not accessible"

                    To my surprise Comeau online and g++ 3.3.1 don't think alike.
                    Thankfully VC 7 agrees with me :-)
                    Am I missing on something?

                    Best wishes,
                    Sharad



                    Comment

                    Working...