Connecting Tech Pros Worldwide Forums | Help | Site Map

enum within class

Alex
Guest
 
Posts: n/a
#1: Aug 18 '05
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.



Martin Vorbrodt
Guest
 
Posts: n/a
#2: Aug 18 '05

re: enum within class


"Alex" <no@domain.com> wrote in message
news:de2kvv$o76$1@nsnmpen2-gest.nuria.telefonica-data.net...[color=blue]
> 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[/color]
don't[color=blue]
> see any advantage of encapsulating this in the enum...
> any reason for this? (I'm beginner, so bear with me)
>
> Thanks.
>
>[/color]

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



Jules
Guest
 
Posts: n/a
#3: Aug 18 '05

re: enum within class


> I'd expect to use the enum this way: X::a . Instead, I have to use[color=blue]
> foo::a Why is this? doesn't the enum create a 'scope' by itself?[/color]

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.
[color=blue]
>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?[/color]

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

Victor Bazarov
Guest
 
Posts: n/a
#4: Aug 18 '05

re: enum within class


Alex wrote:[color=blue]
> 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?[/color]

Nope.
[color=blue]
> 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'...[/color]

What do you mean? 'foo::a' has the type 'foo::X' and 'foo::z' has the
type 'foo::Y'.
[color=blue]
> so I don't
> see any advantage of encapsulating this in the enum...[/color]

Then don't.
[color=blue]
> any reason for this? (I'm beginner, so bear with me)[/color]

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

V
Victor Bazarov
Guest
 
Posts: n/a
#5: Aug 18 '05

re: enum within class


Martin Vorbrodt wrote:[color=blue]
> [...]
> you could always do food::X::a or foo::Y::x[/color]

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

V
Marc Mutz
Guest
 
Posts: n/a
#6: Aug 19 '05

re: enum within class


Jules wrote:
[color=blue]
> the enum's don't need any memory.[/color]

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

Marc

Victor Bazarov
Guest
 
Posts: n/a
#7: Aug 19 '05

re: enum within class


Marc Mutz wrote:[color=blue]
> Jules wrote:
>
>[color=green]
>>the enum's don't need any memory.[/color]
>
>
> static const int = ...; doesn't, either.[/color]

They still need to be defined (read: have storage allocated
for them) if they are used outside the class definition.
Marc Mutz
Guest
 
Posts: n/a
#8: Aug 19 '05

re: enum within class


Victor Bazarov wrote:
<snip>[color=blue][color=green]
>> static const int = ...; doesn't, either.[/color]
>
> They still need to be defined (read: have storage
> allocated for them) if they are used outside the class
> definition.[/color]

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

Victor Bazarov
Guest
 
Posts: n/a
#9: Aug 19 '05

re: enum within class


Marc Mutz wrote:[color=blue]
> Victor Bazarov wrote:
> <snip>
>[color=green][color=darkred]
>>>static const int = ...; doesn't, either.[/color]
>>
>>They still need to be defined (read: have storage
>>allocated for them) if they are used outside the class
>>definition.[/color]
>
>
> 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.[/color]

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
Closed Thread