how to access to enumeration member via it's number

B

braratine

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!
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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);
 
V

Victor Bazarov

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
 
B

braratine

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!
 
D

Dave Rahardja

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
 
V

Victor Bazarov

Dave said:
[...]
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
 
D

Dave Rahardja

Dave said:
[...]
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
 
V

Victor Bazarov

Dave said:
Dave said:
On 20 Feb 2007 05:42:55 -0800, "Erik Wikström"
[...]
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
 
D

Dave Rahardja

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
 
V

Victor Bazarov

Dave said:
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
 
D

Dave Rahardja

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top