473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

enum within class

If I have something like:

class foo {
enum X { a, b, c };
};

Then I'd expect to use the enum this way: X::a . Instead, I have to use
foo::a
Why is this? doesn't the enum create a 'scope' by itself?

The problem is that if I had

class foo {
enum X { a, b, c };
enum Y { x, y, z };
};

Then using foo::a or foo::z don't specify their proper 'type'... so I don't
see any advantage of encapsulating this in the enum...
any reason for this? (I'm beginner, so bear with me)

Thanks.
Aug 18 '05 #1
8 37244
"Alex" <no@domain.com> wrote in message
news:de**********@nsnmpen2-gest.nuria.telefonica-data.net...
If I have something like:

class foo {
enum X { a, b, c };
};

Then I'd expect to use the enum this way: X::a . Instead, I have to use
foo::a
Why is this? doesn't the enum create a 'scope' by itself?

The problem is that if I had

class foo {
enum X { a, b, c };
enum Y { x, y, z };
};

Then using foo::a or foo::z don't specify their proper 'type'... so I don't see any advantage of encapsulating this in the enum...
any reason for this? (I'm beginner, so bear with me)

Thanks.


you could always do food::X::a or foo::Y::x

Aug 18 '05 #2
> I'd expect to use the enum this way: X::a . Instead, I have to use
foo::a Why is this? doesn't the enum create a 'scope' by itself?
No. The main reason for this is for compatibility with C, which
doesn't use scopes like that at all. It's also for convenience --
members of your class 'foo' don't need to specify a scope to use one of
the definitinos.
The problem is that if I had

class foo {
enum X { a, b, c };
enum Y { x, y, z };

};

Then using foo::a or foo::z don't specify their proper 'type'... so I don't
see any advantage of encapsulating this in the enum...
any reason for this?


A few. It enables the compiler to give you an error if you do "foo::X
t; t = foo::z;" for instance. You can also overload functions based on
whether the parameter specified is a foo::X or a foo::Y. Also, the
only practical alternative for making the definitions would be to
declare a, b, c, x, y and z all as 'static const int's, which would
require you to allocate memory for them somewhere: the enum's don't
need any memory.

If you really want to achieve what you describe, you could do it like
this:

class foo {
class X {
enum e { a, b, c };
};
class Y {
enum e { x, y, z };
};
};

You now have to refer to the constants as foo::X::a or foo::Y::z, etc.
unforunately, to declare a variable to hold one, you'll now need to use
"foo::X::e name", instead of "foo::X name".

Aug 18 '05 #3
Alex wrote:
If I have something like:

class foo {
enum X { a, b, c };
};

Then I'd expect to use the enum this way: X::a . Instead, I have to use
foo::a
Why is this? doesn't the enum create a 'scope' by itself?
Nope.
The problem is that if I had

class foo {
enum X { a, b, c };
enum Y { x, y, z };
};

Then using foo::a or foo::z don't specify their proper 'type'...
What do you mean? 'foo::a' has the type 'foo::X' and 'foo::z' has the
type 'foo::Y'.
so I don't
see any advantage of encapsulating this in the enum...
Then don't.
any reason for this? (I'm beginner, so bear with me)


Enums are a carry-over from C. They were made to work like the ones in C.

V
Aug 18 '05 #4
Martin Vorbrodt wrote:
[...]
you could always do food::X::a or foo::Y::x


No, you can't. Neither foo::X nor foo::Y represents a scope.

V
Aug 18 '05 #5
Jules wrote:
the enum's don't need any memory.


static const int = ...; doesn't, either.

Marc

Aug 19 '05 #6
Marc Mutz wrote:
Jules wrote:

the enum's don't need any memory.

static const int = ...; doesn't, either.


They still need to be defined (read: have storage allocated
for them) if they are used outside the class definition.
Aug 19 '05 #7
Victor Bazarov wrote:
<snip>
static const int = ...; doesn't, either.


They still need to be defined (read: have storage
allocated for them) if they are used outside the class
definition.


Hmmmmm... In this case:
struct A {
static const int FOO = 100;
};
the compiler will only allocate storage for FOO if you
take it's address, right? What did you mean with "if used
outside of class definition"? AFAIU,
struct B { int data[A::FOO]; };
doesn't make the compiler allocate storage for A::FOO.

Marc

Aug 19 '05 #8
Marc Mutz wrote:
Victor Bazarov wrote:
<snip>
static const int = ...; doesn't, either.


They still need to be defined (read: have storage
allocated for them) if they are used outside the class
definition.

Hmmmmm... In this case:
struct A {
static const int FOO = 100;
};
the compiler will only allocate storage for FOO if you
take it's address, right? What did you mean with "if used
outside of class definition"? AFAIU,
struct B { int data[A::FOO]; };
doesn't make the compiler allocate storage for A::FOO.


Right... I think there was a defect filed against that at some point.
I am not sure it's made it into 2003 (lazy to check) but generally
speaking you're right. A::FOO becomes a compile-time constant expr..

V
Aug 19 '05 #9

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

Similar topics

3
by: Kenneth | last post by:
I want to define an enum for possible languages in my application. My enum look like this: Public Enum Languages English = 0 Portuguese = 1 End Enum How can i reuse this enum in the rest of...
20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
3
by: developer1996 | last post by:
Can someone please explain to me why you cannot use enum at the method level to define a type. The declaration seems to only work at the class level. From my understanding enums are classified...
1
by: Bill Cohagan | last post by:
I've got an abstract class (say foo) and I'd like to define an Enum (say bar) within that class so that I can then refer to foo.bar.<one of the enum labels> from within other parts of the program....
5
by: Andrea Williams | last post by:
I'm working with C# and I'm setting up some ENUM's I have a data and Business layer. I'm declaring a common enum for the Data Layer. The UI layer references the Bus layer and the bus layer...
6
by: Michael Isaacs | last post by:
Regarding use of enum's, I am wondering what the cost of memory is when creating the enumeration on the calling side, and then using it on the function/method side. See example below. If I...
16
by: Chad | last post by:
In VB6, Enums that I declare within a class as PUBLIC are know outside of this class. In otehr words, the enum is global and may be referenced outside of the class in which it is declared. In...
2
by: Alex Feldman | last post by:
Which of the following is better? Defining an enum type inside a class as a nested type, or in the the namespace? An example of nested type enumerated type would be: public Class Product...
13
by: toton | last post by:
Hi, I have some enum (enumeration ) defined in some namespace, not inside class. How to use the enum constant's in some other namespace without using the whole namespace. To say in little...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.