enum within class

A

Alex

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

Martin Vorbrodt

Alex said:
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
 
V

Victor Bazarov

Alex said:
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
 
J

Jules

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

Victor Bazarov

Marc said:
Jules wrote:




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

Marc Mutz

Victor Bazarov wrote:
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
 
V

Victor Bazarov

Marc said:
Victor Bazarov wrote:
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
 

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,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top