Can I refer to members of an enumeration without using ::?

J

Jason Heyes

I know I can do this with an enum like this:

enum { a, b, c, d };
int x = a + b + c + d;

But what about this one?

enum X { a, b, c, d };
int x = a + b + c + d; // will this compile?

Thanks.
 
H

Howard

Jason Heyes said:
I know I can do this with an enum like this:

enum { a, b, c, d };
int x = a + b + c + d;

But what about this one?

enum X { a, b, c, d };
int x = a + b + c + d; // will this compile?

Thanks.

Simple enough to try for yourself, isn't it?

But yes, it's legal C++. The name of an enumeration is not required for
resolution, except in cases where you have members of different enumerations
having the same name. Such as:

enum enumABC { eMin, eA, eB, eC, eMax };
enum enumXYZ { eMin, eX, eY, eZ, eMax };

In that case, you'd need to specify which enumeration you were referring to
(when referring to eMin or eMax). (But in such a case, putting them in
separate namespaces is also probably a good idea.)

-Howard
 
X

Xenos

Howard said:
But yes, it's legal C++. The name of an enumeration is not required for
resolution, except in cases where you have members of different enumerations
having the same name. Such as:

enum enumABC { eMin, eA, eB, eC, eMax };
enum enumXYZ { eMin, eX, eY, eZ, eMax };

In that case, you'd need to specify which enumeration you were referring to
(when referring to eMin or eMax). (But in such a case, putting them in
separate namespaces is also probably a good idea.)
Wow, you can really do that? That's really cool; I didn't think you could.
I always liked being able to do that in Ada, and wished I could in C++.
I've been doing this:

namespace ABC {
enum abc {eMin, eA, eB, eC, eMax};
}
typedef ABC::abc abc;

namespace XYZ {
enum xyz {eMin, eA, eB, eC, eMax};
}
typedef ABC::abc xyz;


Thanks.
 
X

Xenos

I just tried defining the above enums in a test program, and g++ complained
because they contained the same identifier names for the elements. It this
really legal?
 
H

Howard

Xenos said:
I just tried defining the above enums in a test program, and g++
complained
because they contained the same identifier names for the elements. It
this
really legal?

Apparently I was mistaken on this point. When both names are visible in the
current scope(s), this would amount to illegal name overloading, and won't
compile. They'd have to be in different namespaces (also, I think, within
separate classes would work?)

So my example won't compile. If, however, enumXYZ was in namespace nsXYZ,
then you could use it by specifying nsXYZ::eMin, for example, (which is what
I suggested was probably better to do anyway). But you don't have to
specify the name of the enumeration in order to access its members. This
fact (that the names of the members are not restricted to having the enum
type itself specified) is exactly what makes it illegal to have the same
member name in multiple enums in the same scope: both names are visible at
the scope level, and therefore you can't have two of them declared there!

Sorry about that!

-Howard
 
S

Swampmonster

Howard said:
Apparently I was mistaken on this point. When both names are visible in the
current scope(s), this would amount to illegal name overloading, and won't
compile. They'd have to be in different namespaces (also, I think, within
separate classes would work?)

So my example won't compile. If, however, enumXYZ was in namespace nsXYZ,
then you could use it by specifying nsXYZ::eMin, for example, (which is what
I suggested was probably better to do anyway). But you don't have to
specify the name of the enumeration in order to access its members. This
fact (that the names of the members are not restricted to having the enum
type itself specified) is exactly what makes it illegal to have the same
member name in multiple enums in the same scope: both names are visible at
the scope level, and therefore you can't have two of them declared there!

Sorry about that!

-Howard

AFAIK you can't even qualify a "member of an enum" (correct term?
"enumerator"?) with the enum's name :)

bye,
'monster
 
M

msalters

Jason said:
I know I can do this with an enum like this:

enum { a, b, c, d };
int x = a + b + c + d;

But what about this one?

enum X { a, b, c, d };
int x = a + b + c + d; // will this compile?

Thanks.

Yes. The enumerators are members of the same namespace
(or class) as the enumeration itself, e.g. in your example
if X is really ::NS::X, then a will really be ::NS::a - not ::NS::X::a.
You therefore can't even use X::a.

There are ideas to add something like X::a to C++, but
for now to get an enumerator called X::a, you have
to make X a namespace (or class):

namespace X {
enum X{ a,b,c,d };
}
X::X x = X::a;

HTH,
Michiel Salters
 

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

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top