473,698 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.
Jul 22 '05 #1
7 1792
yes

Stephan

Jul 22 '05 #2

"Jason Heyes" <ja********@opt usnet.com.au> wrote in message
news:41******** **************@ news.optusnet.c om.au...
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

Jul 22 '05 #3

"Howard" <al*****@hotmai l.com> wrote in message
news:oe******** **************@ bgtnsc04-news.ops.worldn et.att.net...

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.
Jul 22 '05 #4

"Xenos" <do**********@s pamhate.com> wrote in message
news:cp******** *@cui1.lmms.lmc o.com...

"Howard" <al*****@hotmai l.com> wrote in message
news:oe******** **************@ bgtnsc04-news.ops.worldn et.att.net...

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.)


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?
Jul 22 '05 #5

"Xenos" <do**********@s pamhate.com> wrote in message
news:cp******** *@cui1.lmms.lmc o.com...

"Xenos" <do**********@s pamhate.com> wrote in message
news:cp******** *@cui1.lmms.lmc o.com...

"Howard" <al*****@hotmai l.com> wrote in message
news:oe******** **************@ bgtnsc04-news.ops.worldn et.att.net...
>
> 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.)
>


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


Jul 22 '05 #6
Howard wrote:
"Xenos" <do**********@s pamhate.com> wrote in message
news:cp******** *@cui1.lmms.lmc o.com...
"Xenos" <do**********@s pamhate.com> wrote in message
news:cp****** ***@cui1.lmms.l mco.com...
"Howard" <al*****@hotmai l.com> wrote in message
news:oe***** *************** **@bgtnsc04-news.ops.worldn et.att.net...

But yes, it's legal C++. The name of an enumeration is not required
for
resolutio n, except in cases where you have members of different

enumeratio ns

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.)


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


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

bye,
'monster
Jul 22 '05 #7

Jason Heyes wrote:
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

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2459
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
1
3666
by: Sergey Poberezovskiy | last post by:
Hi, I have a simple enumeration in my schema: <xs:element name="el_1"> <xs:simpleType> <xs:restiction base="xs:string"> <xs:enumeration value="value and space 1"/> <xs:enumeration value="value2 with spaces"/> ...
21
6342
by: Bilgehan.Balban | last post by:
Hi, I have two different enum definitions with members of same name. The compiler complains about duplicate definitions. Is this expected behaviour? Can't I have same-named fields in different enum definitions? I think it should have been perfectly valid to do that. Its a stupid limitation to have to define disjoint enum symbol definitions. This is like having to have disjoint member names in structures.
5
14430
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with the class and this works but I would also like to know of other ways to do this. Also are there any performance implacations of using sealed?
5
15888
by: Ken Allen | last post by:
I have a need to convert the names of the members of an enumeration into an array of strings at runtime. I have determined a method using reflection. Type theType = typeof(MyEnumName); MemberInfo theMembers = theType.getMembers(); foreach (MemberInfo entry in theMembers) {
4
5658
by: Marshal | last post by:
Sure... IEnumerable was inconvenient suggesting a separate class to service the enumeration, IEnumerator, and multiple operations: Current, MoveNext, Reset. (I'll warp the definition of "operation" for a second if you don't mind). However, it existed within intuitive language semantics, whereas the new "yield" keyword, while highly convenient, is also one of the most gross warping of language concepts to date... public IEnumerator...
0
1600
by: Larry Lard | last post by:
This came out of a thread explaining to "BK" about error BC42025 ("Access of shared member through an instance; qualifying expression will not be evaluated"); Frans Clasener then came up with another similar problem, which I believe shows up a bug (well, a problem) in VB2005. As many will know, VB2005 saw the reintroduction of the 'default instance' of Form classes, allowing one to write code such as Form1.Show
0
1856
by: puvit82 | last post by:
My problem is as follows, any advice / suggestion would be greatly appreciated: Lets suppose that I have defined a simpleType "addressType" with 3 enumeration values (Home, Office, Vacation) that restrict data entry. I want to use this simpleType in 2 different unrelated complexType definitions, lets call them "personalAddress" and "companyAddress" How will I be able to use "addressType" inside "personalAddress" such that the 3...
1
2415
by: Visame | last post by:
The following example is from C++ Standard(ISO14882) 9.4.2 Static data members class process { static process* run_chain; static process* running; }; process* process::running = get_main();//what does get_main() mean here? process* process::run_chain = running; C++ Standard also says in:
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.