how to access to enumeration member via it's number

Discussion in 'C++' started by braratine@gmail.com, Feb 20, 2007.

  1. Guest

    Hello,
    I have to following issue.
    A type is declared based on an enum:
    "
    enum TpAddressPlan {
    P_ADDRESS_PLAN_NOT_PRESENT = 0,
    P_ADDRESS_PLAN_UNDEFINED = 1,
    P_ADDRESS_PLAN_IP = 2,
    P_ADDRESS_PLAN_MULTICAST = 3,
    P_ADDRESS_PLAN_UNICAST = 4,
    P_ADDRESS_PLAN_E164 = 5,
    P_ADDRESS_PLAN_AESA = 6,
    P_ADDRESS_PLAN_URL = 7,
    P_ADDRESS_PLAN_NSAP = 8,
    P_ADDRESS_PLAN_SMTP = 9,
    P_ADDRESS_PLAN_MSMAIL = 10,
    P_ADDRESS_PLAN_X400 = 11,
    P_ADDRESS_PLAN_SIP = 12,
    P_ADDRESS_PLAN_ANY = 13,
    P_ADDRESS_PLAN_MIN = 14
    };
    typedef enum TpAddressPlan TpAddressPlan;
    "

    And a function receive an integer corresponding to the enumeration
    member that I must convert into the member to fill a structure member
    of the type TpAddressPlan:
    "
    this->msg.CEType_u.msgreq.CERequest_u.qryreq.userID.Plan =
    AddressPlan; // [0-14]
    "

    How can I assign to AddressPlan the enumeration value, starting from
    it's integer value: kind of

    ???AddressPlan = TpAddressPlan (i)???

    Thanks for your help!
     
    , Feb 20, 2007
    #1
    1. Advertising

  2. On Feb 20, 2:18 pm, wrote:
    > Hello,
    > I have to following issue.
    > A type is declared based on an enum:
    > "
    > enum TpAddressPlan {
    > P_ADDRESS_PLAN_NOT_PRESENT = 0,
    > P_ADDRESS_PLAN_UNDEFINED = 1,
    > P_ADDRESS_PLAN_IP = 2,
    > P_ADDRESS_PLAN_MULTICAST = 3,
    > P_ADDRESS_PLAN_UNICAST = 4,
    > P_ADDRESS_PLAN_E164 = 5,
    > P_ADDRESS_PLAN_AESA = 6,
    > P_ADDRESS_PLAN_URL = 7,
    > P_ADDRESS_PLAN_NSAP = 8,
    > P_ADDRESS_PLAN_SMTP = 9,
    > P_ADDRESS_PLAN_MSMAIL = 10,
    > P_ADDRESS_PLAN_X400 = 11,
    > P_ADDRESS_PLAN_SIP = 12,
    > P_ADDRESS_PLAN_ANY = 13,
    > P_ADDRESS_PLAN_MIN = 14};
    >
    > typedef enum TpAddressPlan TpAddressPlan;


    You don't need a typedef in C++.


    > And a function receive an integer corresponding to the enumeration
    > member that I must convert into the member to fill a structure member
    > of the type TpAddressPlan:
    > "
    > this->msg.CEType_u.msgreq.CERequest_u.qryreq.userID.Plan =
    > AddressPlan; // [0-14]
    > "
    >
    > How can I assign to AddressPlan the enumeration value, starting from
    > it's integer value: kind of
    >
    > ???AddressPlan = TpAddressPlan (i)???


    If you are sure that i is in the correct range (0-14) and the value
    corresponds to the right enumeration then just use

    AddressPlan = TpAddressPlan(i);

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 20, 2007
    #2
    1. Advertising

  3. wrote:
    > I have to following issue.
    > A type is declared based on an enum:
    > "
    > enum TpAddressPlan {
    > P_ADDRESS_PLAN_NOT_PRESENT = 0,
    > P_ADDRESS_PLAN_UNDEFINED = 1,
    > P_ADDRESS_PLAN_IP = 2,
    > P_ADDRESS_PLAN_MULTICAST = 3,
    > P_ADDRESS_PLAN_UNICAST = 4,
    > P_ADDRESS_PLAN_E164 = 5,
    > P_ADDRESS_PLAN_AESA = 6,
    > P_ADDRESS_PLAN_URL = 7,
    > P_ADDRESS_PLAN_NSAP = 8,
    > P_ADDRESS_PLAN_SMTP = 9,
    > P_ADDRESS_PLAN_MSMAIL = 10,
    > P_ADDRESS_PLAN_X400 = 11,
    > P_ADDRESS_PLAN_SIP = 12,
    > P_ADDRESS_PLAN_ANY = 13,
    > P_ADDRESS_PLAN_MIN = 14
    > };
    > typedef enum TpAddressPlan TpAddressPlan;


    That's unnecessary. 'enum' in variable declarations is optional.

    > "
    >
    > And a function receive an integer corresponding to the enumeration
    > member that I must convert into the member to fill a structure member
    > of the type TpAddressPlan:
    > "
    > this->msg.CEType_u.msgreq.CERequest_u.qryreq.userID.Plan =
    > AddressPlan; // [0-14]
    > "
    >
    > How can I assign to AddressPlan the enumeration value, starting from
    > it's integer value: kind of
    >
    > ???AddressPlan = TpAddressPlan (i)???


    That's exactly how you do it.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 20, 2007
    #3
  4. Guest

    Thanks a lot!!! all.

    I wrote "AddressPlan = TpAddressPlan"!! That was the issue!
    Anyway, I didn't knew that "AddressPlan = TpAddressPlan(i)" would
    work.

    Thanks a lot!
     
    , Feb 20, 2007
    #4
  5. On 20 Feb 2007 05:42:55 -0800, "Erik Wikström" <>
    wrote:

    >On Feb 20, 2:18 pm, wrote:
    >> Hello,
    >> I have to following issue.
    >> A type is declared based on an enum:
    >> "
    >> enum TpAddressPlan {
    >> P_ADDRESS_PLAN_NOT_PRESENT = 0,
    >> P_ADDRESS_PLAN_UNDEFINED = 1,
    >> P_ADDRESS_PLAN_IP = 2,
    >> P_ADDRESS_PLAN_MULTICAST = 3,
    >> P_ADDRESS_PLAN_UNICAST = 4,
    >> P_ADDRESS_PLAN_E164 = 5,
    >> P_ADDRESS_PLAN_AESA = 6,
    >> P_ADDRESS_PLAN_URL = 7,
    >> P_ADDRESS_PLAN_NSAP = 8,
    >> P_ADDRESS_PLAN_SMTP = 9,
    >> P_ADDRESS_PLAN_MSMAIL = 10,
    >> P_ADDRESS_PLAN_X400 = 11,
    >> P_ADDRESS_PLAN_SIP = 12,
    >> P_ADDRESS_PLAN_ANY = 13,
    >> P_ADDRESS_PLAN_MIN = 14};
    >>
    >> typedef enum TpAddressPlan TpAddressPlan;

    >
    >You don't need a typedef in C++.
    >
    >
    >> And a function receive an integer corresponding to the enumeration
    >> member that I must convert into the member to fill a structure member
    >> of the type TpAddressPlan:
    >> "
    >> this->msg.CEType_u.msgreq.CERequest_u.qryreq.userID.Plan =
    >> AddressPlan; // [0-14]
    >> "
    >>
    >> How can I assign to AddressPlan the enumeration value, starting from
    >> it's integer value: kind of
    >>
    >> ???AddressPlan = TpAddressPlan (i)???

    >
    >If you are sure that i is in the correct range (0-14) and the value
    >corresponds to the right enumeration then just use
    >
    >AddressPlan = TpAddressPlan(i);


    I've never seen this expression form before. Where is it specified in the
    standard?

    -dr
     
    Dave Rahardja, Feb 20, 2007
    #5
  6. Dave Rahardja wrote:
    > On 20 Feb 2007 05:42:55 -0800, "Erik Wikström"
    > <> wrote:
    >> [...]
    >> If you are sure that i is in the correct range (0-14) and the value
    >> corresponds to the right enumeration then just use
    >>
    >> AddressPlan = TpAddressPlan(i);

    >
    > I've never seen this expression form before. Where is it specified in
    > the standard?


    5.2.3

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 20, 2007
    #6
  7. On Tue, 20 Feb 2007 09:39:43 -0500, "Victor Bazarov" <>
    wrote:

    >Dave Rahardja wrote:
    >> On 20 Feb 2007 05:42:55 -0800, "Erik Wikström"
    >> <> wrote:
    >>> [...]
    >>> If you are sure that i is in the correct range (0-14) and the value
    >>> corresponds to the right enumeration then just use
    >>>
    >>> AddressPlan = TpAddressPlan(i);

    >>
    >> I've never seen this expression form before. Where is it specified in
    >> the standard?

    >
    >5.2.3


    Of course. Somehow I misunderstood what the OP meant. I thought there was a
    notation whereby you can retrieve the n'th constant in an enumeration, e.g.

    enum E
    {
    FOO = 2,
    BAR = 4,
    BAZ = 9
    };

    E e = E[1]; // e == 4

    Which is ill-formed.

    -dr
     
    Dave Rahardja, Feb 22, 2007
    #7
  8. Dave Rahardja wrote:
    > On Tue, 20 Feb 2007 09:39:43 -0500, "Victor Bazarov"
    > <> wrote:
    >
    >> Dave Rahardja wrote:
    >>> On 20 Feb 2007 05:42:55 -0800, "Erik Wikström"
    >>> <> wrote:
    >>>> [...]
    >>>> If you are sure that i is in the correct range (0-14) and the value
    >>>> corresponds to the right enumeration then just use
    >>>>
    >>>> AddressPlan = TpAddressPlan(i);
    >>>
    >>> I've never seen this expression form before. Where is it specified
    >>> in the standard?

    >>
    >> 5.2.3

    >
    > Of course. Somehow I misunderstood what the OP meant. I thought there
    > was a notation whereby you can retrieve the n'th constant in an
    > enumeration, e.g.
    >
    > enum E
    > {
    > FOO = 2,
    > BAR = 4,
    > BAZ = 9
    > };
    >
    > E e = E[1]; // e == 4
    >
    > Which is ill-formed.


    Well, right, there is no way. That's why there are several "smart
    enum" implementations out there. Essentially, what's recommended is
    to implement your own type with conversions to 'int' (if you need
    those) and with named constants of that type. If you do, you have
    the chance to define your own operator[] which would give you the
    indexed value. OTOH, using indexing with enumerations does not
    actually follow the ideology behind enumerations. What you have
    here is not an enumeration (named constants) but an array of some
    values which also have tags. You should perhaps consider

    const int somearray[] = { 2, 4, 9 };
    const int& FOO = somearray[0];
    const int& BAR = somearray[1];
    const int& BAZ = somearray[2];

    (or even wrapping it into a struct named 'E'...)

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 22, 2007
    #8
  9. On Thu, 22 Feb 2007 09:27:57 -0500, "Victor Bazarov" <>
    wrote:

    >> Of course. Somehow I misunderstood what the OP meant. I thought there
    >> was a notation whereby you can retrieve the n'th constant in an
    >> enumeration, e.g.
    >>
    >> enum E
    >> {
    >> FOO = 2,
    >> BAR = 4,
    >> BAZ = 9
    >> };
    >>
    >> E e = E[1]; // e == 4
    >>
    >> Which is ill-formed.

    >
    >Well, right, there is no way. That's why there are several "smart
    >enum" implementations out there. Essentially, what's recommended is
    >to implement your own type with conversions to 'int' (if you need
    >those) and with named constants of that type. If you do, you have
    >the chance to define your own operator[] which would give you the
    >indexed value. OTOH, using indexing with enumerations does not
    >actually follow the ideology behind enumerations. What you have
    >here is not an enumeration (named constants) but an array of some
    >values which also have tags. You should perhaps consider
    >
    > const int somearray[] = { 2, 4, 9 };
    > const int& FOO = somearray[0];
    > const int& BAR = somearray[1];
    > const int& BAZ = somearray[2];
    >
    >(or even wrapping it into a struct named 'E'...)


    Yes, but with one difference: an indexed enumeration would be strictly a
    compile-time construct. You can't take its address, and you can't use a
    run-time variable as the index.

    I smell a template metaprogram coming on...

    -dr
     
    Dave Rahardja, Feb 23, 2007
    #9
  10. Dave Rahardja wrote:
    > On Thu, 22 Feb 2007 09:27:57 -0500, "Victor Bazarov"
    > <> wrote:
    >
    >>> Of course. Somehow I misunderstood what the OP meant. I thought
    >>> there was a notation whereby you can retrieve the n'th constant in
    >>> an enumeration, e.g.
    >>>
    >>> enum E
    >>> {
    >>> FOO = 2,
    >>> BAR = 4,
    >>> BAZ = 9
    >>> };
    >>>
    >>> E e = E[1]; // e == 4
    >>>
    >>> Which is ill-formed.

    >>
    >> Well, right, there is no way. That's why there are several "smart
    >> enum" implementations out there. Essentially, what's recommended is
    >> to implement your own type with conversions to 'int' (if you need
    >> those) and with named constants of that type. If you do, you have
    >> the chance to define your own operator[] which would give you the
    >> indexed value. OTOH, using indexing with enumerations does not
    >> actually follow the ideology behind enumerations. What you have
    >> here is not an enumeration (named constants) but an array of some
    >> values which also have tags. You should perhaps consider
    >>
    >> const int somearray[] = { 2, 4, 9 };
    >> const int& FOO = somearray[0];
    >> const int& BAR = somearray[1];
    >> const int& BAZ = somearray[2];
    >>
    >> (or even wrapping it into a struct named 'E'...)

    >
    > Yes, but with one difference: an indexed enumeration would be
    > strictly a compile-time construct. You can't take its address, and
    > you can't use a run-time variable as the index.
    >
    > I smell a template metaprogram coming on...


    It might be an interesting exercise, but I honestly don't see any
    practical value in it. Do you?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 23, 2007
    #10
  11. On Fri, 23 Feb 2007 08:21:18 -0500, "Victor Bazarov" <>
    wrote:

    >> Yes, but with one difference: an indexed enumeration would be
    >> strictly a compile-time construct. You can't take its address, and
    >> you can't use a run-time variable as the index.
    >>
    >> I smell a template metaprogram coming on...

    >
    >It might be an interesting exercise, but I honestly don't see any
    >practical value in it. Do you?


    Sure, as a lookup table at compile time. Come to think of it, it's much easier
    to use boost::mpl::vector_c<int, ...> to achieve this.

    -dr
     
    Dave Rahardja, Feb 23, 2007
    #11
    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. Tim
    Replies:
    1
    Views:
    485
    Gary van der Merwe
    Oct 23, 2003
  2. ding feng
    Replies:
    8
    Views:
    874
    Dhruv
    Jul 2, 2003
  3. Neil Zanella
    Replies:
    4
    Views:
    424
    Richard Herring
    Apr 13, 2004
  4. Alexander Eisenhuth

    Howto access a enumeration in a COM TypeLib

    Alexander Eisenhuth, Jun 21, 2005, in forum: Python
    Replies:
    2
    Views:
    483
    Alexander Eisenhuth
    Jun 21, 2005
  5. puvit82
    Replies:
    4
    Views:
    781
    puvit82
    Feb 1, 2008
Loading...

Share This Page