enum !

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

    enum !

    Hi,
    I have a code in which I have an enum like

    enum ADDR
    {
    one = 0;
    two = 1;
    three = 2;
    };

    Later in code , I have a function that returns an ADDR type, which
    returns an integer referring to the correct value but I want to print
    out the name one,two or three.
    How can I do that, my example has a lot of cases so dont want to use a
    switch statement.

    Thanks,
    Kapil
  • Josephine Schafer

    #2
    Re: enum !


    "Kapil Khosla" <khoslakapil@ya hoo.com> wrote in message
    news:919aa2da.0 307062325.7593c 978@posting.goo gle.com...[color=blue]
    > Hi,
    > I have a code in which I have an enum like
    >
    > enum ADDR
    > {
    > one = 0;
    > two = 1;
    > three = 2;
    > };
    >
    > Later in code , I have a function that returns an ADDR type, which
    > returns an integer referring to the correct value but I want to print
    > out the name one,two or three.
    > How can I do that, my example has a lot of cases so dont want to use a
    > switch statement.
    >[/color]

    AFAIK it's not achievable in your case (with an enum).
    Why not use a map?

    With best wishes,
    J.Schafer


    Comment

    • Rolf Magnus

      #3
      Re: enum !

      Kapil Khosla wrote:
      [color=blue]
      > Hi,
      > I have a code in which I have an enum like
      >
      > enum ADDR
      > {
      > one = 0;
      > two = 1;
      > three = 2;
      > };
      >
      > Later in code , I have a function that returns an ADDR type, which
      > returns an integer referring to the correct value but I want to print
      > out the name one,two or three.
      > How can I do that, my example has a lot of cases so dont want to use a
      > switch statement.[/color]

      You can make an array of the string representations :

      const char* ADDR_strings[] = { "one", "two", "three" };

      Then you can do something like:

      ADDR addr = get_the_addr();
      std::cout << ADDR_strings[addr] << std::endl;

      Comment

      • Georg Krichel

        #4
        Re: enum !

        "Kapil Khosla" <khoslakapil@ya hoo.com> schrieb im Newsbeitrag
        news:919aa2da.0 307062325.7593c 978@posting.goo gle.com...[color=blue]
        > Hi,
        > I have a code in which I have an enum like
        >
        > enum ADDR
        > {
        > one = 0;
        > two = 1;
        > three = 2;
        > };
        >
        > Later in code , I have a function that returns an ADDR type, which
        > returns an integer referring to the correct value but I want to print
        > out the name one,two or three.
        > How can I do that, my example has a lot of cases so dont want to use a
        > switch statement.
        >
        > Thanks,
        > Kapil[/color]

        I dont' know any solution without a 'switch' or a std::map with an insert
        for each enumerator.
        In my applications I use something like this:

        std::ostream& operator <<( std::ostream& o, ADDR e )
        {
        switch( e )
        {
        case one: return o << "one";
        case two: return o << "two";
        case three: return o << "three";
        }
        }

        Georg



        Comment

        • yvan joffre

          #5
          Re: enum !

          khoslakapil@yah oo.com (Kapil Khosla) wrote in message news:<919aa2da. 0307062325.7593 c978@posting.go ogle.com>...[color=blue]
          > Hi,
          > I have a code in which I have an enum like
          >
          > enum ADDR
          > {
          > one = 0;
          > two = 1;
          > three = 2;
          > };
          >
          > Later in code , I have a function that returns an ADDR type, which
          > returns an integer referring to the correct value but I want to print
          > out the name one,two or three.
          > How can I do that, my example has a lot of cases so dont want to use a
          > switch statement.
          >
          > Thanks,
          > Kapil[/color]


          Can you modify the definition of ADDR? If yes, you can try the
          following code :
          #define FOR_ALL_ADDR(P) P(one) P(two) P(three)
          #define DECLARE_ADDR(A) A,

          enum ADDR {
          FOR_ALL_ADDR(DE CLARE_ADDR) ADDR_COUNT
          };

          #define MAP_ADDR_TO_STR ING(A) #A,
          const char *const AddrStr[ADDR_COUNT] = {
          FOR_ALL_ADDR(MA P_ADDR_TO_STRIN G)
          };

          Then, you index directly the array AddrStr with an ADDR value to
          retrieve the associated string (for instance AddrStr[one] is equals to
          "one").

          Comment

          • Jeremy Cowles

            #6
            Re: enum !

            "Chris ( Val )" <chrisval@bigpo nd.com.au> wrote in message
            news:beblea$388 qd$1@ID-110726.news.dfn cis.de...[color=blue]
            >
            > "Kapil Khosla" <khoslakapil@ya hoo.com> wrote in message
            > news:919aa2da.0 307062325.7593c 978@posting.goo gle.com...
            > | Hi,[/color]

            [snip]
            [color=blue]
            > int main()
            > {
            > typedef std::map<ADDR, std::string> MyMap;
            >
            > MyMap M;[/color]


            Couldn't you use the following code, eliminating the need for a typedef?

            std::map<ADDR, std::string> M;

            Or do you create a typedef for later use so you don't have to keep writing
            out the template parameters?

            Thanks,
            Jeremy

            Comment

            • Kapil Khosla

              #7
              Re: enum !

              Thanks a lot guys !
              I worked out my solution by using the MAP STL and was pretty interesting.
              Kapil

              Comment

              • Chris \( Val \)

                #8
                Re: enum !


                "Jeremy Cowles" <jeremy.stop-spam-now.cowles@asif l.com> wrote in message news:ktgOa.6648 0

                Hi Jeremy.

                $ic1.952880@twi ster.tampabay.r r.com...
                | "Chris ( Val )" <chrisval@bigpo nd.com.au> wrote in message
                | news:beblea$388 qd$1@ID-110726.news.dfn cis.de...
                | >
                | > "Kapil Khosla" <khoslakapil@ya hoo.com> wrote in message
                | > news:919aa2da.0 307062325.7593c 978@posting.goo gle.com...
                | > | Hi,
                |
                | [snip]
                |
                | > int main()
                | > {
                | > typedef std::map<ADDR, std::string> MyMap;
                | >
                | > MyMap M;
                |
                |
                | Couldn't you use the following code, eliminating the need for a typedef?
                |
                | std::map<ADDR, std::string> M;

                Yes, of course :-).

                | Or do you create a typedef for later use so you don't have to keep writing
                | out the template parameters?

                Correct, but mainly(for this exercise), to *avoid* stuff like this:

                MyMap.insert( std::map<ADDR, std::string>::v alue_type( one, "one" ) );
                MyMap.insert( std::map<ADDR, std::string>::v alue_type( two, "two" ) );
                MyMap.insert( std::map<ADDR, std::string>::v alue_type( three, "three" ) );

                ....rather than:

                M.insert( MyMap::value_ty pe( one, "one" ) );
                M.insert( MyMap::value_ty pe( two, "two" ) );
                M.insert( MyMap::value_ty pe( three, "three" ) );

                See the difference ?

                Cheers.
                Chris Val


                Comment

                • Jeremy Cowles

                  #9
                  Re: enum !


                  "Chris ( Val )" <chrisval@bigpo nd.com.au> wrote in message
                  news:bedskq$40l uv$1@ID-110726.news.dfn cis.de...[color=blue]
                  >
                  > "Jeremy Cowles" <jeremy.stop-spam-now.cowles@asif l.com> wrote in message[/color]
                  news:ktgOa.6648 0[color=blue]
                  >
                  > Hi Jeremy.
                  >
                  > $ic1.952880@twi ster.tampabay.r r.com...
                  > | "Chris ( Val )" <chrisval@bigpo nd.com.au> wrote in message
                  > | news:beblea$388 qd$1@ID-110726.news.dfn cis.de...
                  > | >
                  > | > "Kapil Khosla" <khoslakapil@ya hoo.com> wrote in message
                  > | > news:919aa2da.0 307062325.7593c 978@posting.goo gle.com...
                  > | > | Hi,
                  > |
                  > | [snip]
                  > |
                  > | > int main()
                  > | > {
                  > | > typedef std::map<ADDR, std::string> MyMap;
                  > | >
                  > | > MyMap M;
                  > |
                  > |
                  > | Couldn't you use the following code, eliminating the need for a typedef?
                  > |
                  > | std::map<ADDR, std::string> M;
                  >
                  > Yes, of course :-).
                  >
                  > | Or do you create a typedef for later use so you don't have to keep[/color]
                  writing[color=blue]
                  > | out the template parameters?
                  >
                  > Correct, but mainly(for this exercise), to *avoid* stuff like this:
                  >
                  > MyMap.insert( std::map<ADDR, std::string>::v alue_type( one, "one" ) );
                  > MyMap.insert( std::map<ADDR, std::string>::v alue_type( two, "two" ) );
                  > MyMap.insert( std::map<ADDR, std::string>::v alue_type( three,[/color]
                  "three" ) );[color=blue]
                  >
                  > ...rather than:
                  >
                  > M.insert( MyMap::value_ty pe( one, "one" ) );
                  > M.insert( MyMap::value_ty pe( two, "two" ) );
                  > M.insert( MyMap::value_ty pe( three, "three" ) );
                  >
                  > See the difference ?
                  >[/color]


                  Yes, thanks.

                  Comment

                  • Jerry Coffin

                    #10
                    Re: enum !

                    In article <bedskq$40luv$1 @ID-110726.news.dfn cis.de>, "Chris \( Val \)"
                    <chrisval@bigpo nd.com.au> says...

                    [ ... ]
                    [color=blue]
                    > Correct, but mainly(for this exercise), to *avoid* stuff like this:
                    >
                    > MyMap.insert( std::map<ADDR, std::string>::v alue_type( one, "one" ) );
                    > MyMap.insert( std::map<ADDR, std::string>::v alue_type( two, "two" ) );
                    > MyMap.insert( std::map<ADDR, std::string>::v alue_type( three, "three" ) );
                    >
                    > ...rather than:
                    >
                    > M.insert( MyMap::value_ty pe( one, "one" ) );
                    > M.insert( MyMap::value_ty pe( two, "two" ) );
                    > M.insert( MyMap::value_ty pe( three, "three" ) );
                    >
                    > See the difference ?[/color]

                    For this particular set of circumstances, I'd use:

                    M[one] = "one";
                    M[two] = "two";
                    M[three] = "three";

                    Which is not only shorter still, but strikes me as considerably more
                    readable as well. Of course, it doesn't apply to all the possible
                    collection types, but for the job at hand it works very nicely.

                    --
                    Later,
                    Jerry.

                    The universe is a figment of its own imagination.

                    Comment

                    • Chris \( Val \)

                      #11
                      Re: enum !


                      "Jerry Coffin" <jcoffin@taeus. com> wrote in message
                      news:MPG.197481 1e6d4859b9989ab 4@news.clspco.a delphia.net...
                      | In article <bedskq$40luv$1 @ID-110726.news.dfn cis.de>, "Chris \( Val \)"
                      | <chrisval@bigpo nd.com.au> says...
                      |
                      | [ ... ]
                      |
                      | > Correct, but mainly(for this exercise), to *avoid* stuff like this:
                      | >
                      | > MyMap.insert( std::map<ADDR, std::string>::v alue_type( one, "one" ) );
                      | > MyMap.insert( std::map<ADDR, std::string>::v alue_type( two, "two" ) );
                      | > MyMap.insert( std::map<ADDR, std::string>::v alue_type( three, "three" ) );
                      | >
                      | > ...rather than:
                      | >
                      | > M.insert( MyMap::value_ty pe( one, "one" ) );
                      | > M.insert( MyMap::value_ty pe( two, "two" ) );
                      | > M.insert( MyMap::value_ty pe( three, "three" ) );
                      | >
                      | > See the difference ?
                      |
                      | For this particular set of circumstances, I'd use:
                      |
                      | M[one] = "one";
                      | M[two] = "two";
                      | M[three] = "three";
                      |
                      | Which is not only shorter still, but strikes me as considerably more
                      | readable as well. Of course, it doesn't apply to all the possible
                      | collection types, but for the job at hand it works very nicely.

                      Hi Jerry.

                      Agreed, but as I said, I'm trying to get into the habit of
                      using 'value_type' :-).

                      Cheers.
                      Chris Val


                      Comment

                      • Jerry Coffin

                        #12
                        Re: enum !

                        In article <begmff$4ttd3$1 @ID-110726.news.dfn cis.de>, "Chris \( Val \)"
                        <chrisval@bigpo nd.com.au> says...

                        [ ... ]
                        [color=blue]
                        > Hi Jerry.[/color]

                        Hi Chris,
                        [color=blue]
                        > Agreed, but as I said, I'm trying to get into the habit of
                        > using 'value_type' :-).[/color]

                        If I might venture an opinion, I'd advance one habit that I think should
                        outweigh the rest: writing the best code you know how in any given
                        situation. Particular techniques are useful to the degree that they
                        help you program better, but (still IMO, of course) should be ignored
                        when they get in the way of programming as well as possible.

                        OTOH, there's no real question that when you learn a new technique, it's
                        sometimes easy to overdo it a bit. At times, it's even useful to do so,
                        because it helps you explore not only the uses, but also the limits and
                        weaknesses of that technique -- and 2:00 AM the morning before the big
                        demo is NOT the best time for "educationa l experiences" about the limits
                        of a technique you've been counting on to make things work! <G>

                        --
                        Later,
                        Jerry.

                        The universe is a figment of its own imagination.

                        Comment

                        Working...