By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

how to access to enumeration member via it's number

P: n/a
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.Pla n =
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 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On Feb 20, 2:18 pm, brarat...@gmail.com 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.Pla n =
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

Feb 20 '07 #2

P: n/a
br*******@gmail.com 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.Pla n =
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
Feb 20 '07 #3

P: n/a


Thanks a lot!!! all.

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

Thanks a lot!

Feb 20 '07 #4

P: n/a
On 20 Feb 2007 05:42:55 -0800, "Erik Wikström" <er****@student.chalmers.se>
wrote:
>On Feb 20, 2:18 pm, brarat...@gmail.com 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.Pla n =
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
Feb 20 '07 #5

P: n/a
Dave Rahardja wrote:
On 20 Feb 2007 05:42:55 -0800, "Erik Wikström"
<er****@student.chalmers.sewrote:
>[...]
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
Feb 20 '07 #6

P: n/a
On Tue, 20 Feb 2007 09:39:43 -0500, "Victor Bazarov" <v.********@comAcast.net>
wrote:
>Dave Rahardja wrote:
>On 20 Feb 2007 05:42:55 -0800, "Erik Wikström"
<er****@student.chalmers.sewrote:
>>[...]
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
Feb 22 '07 #7

P: n/a
Dave Rahardja wrote:
On Tue, 20 Feb 2007 09:39:43 -0500, "Victor Bazarov"
<v.********@comAcast.netwrote:
>Dave Rahardja wrote:
>>On 20 Feb 2007 05:42:55 -0800, "Erik Wikström"
<er****@student.chalmers.sewrote:
[...]
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
Feb 22 '07 #8

P: n/a
On Thu, 22 Feb 2007 09:27:57 -0500, "Victor Bazarov" <v.********@comAcast.net>
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
Feb 23 '07 #9

P: n/a
Dave Rahardja wrote:
On Thu, 22 Feb 2007 09:27:57 -0500, "Victor Bazarov"
<v.********@comAcast.netwrote:
>>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
Feb 23 '07 #10

P: n/a
On Fri, 23 Feb 2007 08:21:18 -0500, "Victor Bazarov" <v.********@comAcast.net>
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
Feb 23 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.