enum within class

Discussion in 'C++' started by Alex, Aug 18, 2005.

  1. Alex

    Alex Guest

    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.
    Alex, Aug 18, 2005
    #1
    1. Advertising

  2. "Alex" <> wrote in message
    news:de2kvv$o76$-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
    Martin Vorbrodt, Aug 18, 2005
    #2
    1. Advertising

  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
    Victor Bazarov, Aug 18, 2005
    #3
  4. Alex

    Jules Guest

    > 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".
    Jules, Aug 18, 2005
    #4
  5. 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
    Victor Bazarov, Aug 18, 2005
    #5
  6. Alex

    Marc Mutz Guest

    Jules wrote:

    > the enum's don't need any memory.


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

    Marc
    Marc Mutz, Aug 19, 2005
    #6
  7. 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.
    Victor Bazarov, Aug 19, 2005
    #7
  8. Alex

    Marc Mutz Guest

    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
    Marc Mutz, Aug 19, 2005
    #8
  9. 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
    Victor Bazarov, Aug 19, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    533
  2. Jerminia
    Replies:
    3
    Views:
    622
    Roedy Green
    Oct 7, 2005
  3. mrhicks
    Replies:
    2
    Views:
    414
    Dave Thompson
    Jun 10, 2004
  4. Brian
    Replies:
    4
    Views:
    2,617
    Brian
    Feb 27, 2010
  5. Ansel

    "enum" vs. "enum class"

    Ansel, Aug 26, 2012, in forum: C++
    Replies:
    16
    Views:
    742
    Ansel
    Aug 27, 2012
Loading...

Share This Page