enum in class

Discussion in 'C++' started by Teddy, May 21, 2005.

  1. Teddy

    Teddy Guest

    what is the special meaning of enum members in class ?
    Teddy, May 21, 2005
    #1
    1. Advertising

  2. Teddy wrote:
    > what is the special meaning of enum members in class ?
    >


    An example:

    --- en.cpp ---

    // a global enum named MyColor
    enum MyColor { Red, Green, Blue };

    // a simple class with an enum and a variable of that
    // enum type
    class Test
    {
    public:
    enum TestEnum
    {
    value1,
    value2
    };

    TestEnum enumVar;
    };

    int main()
    {
    // a variable named 'clr' of type 'MyColor'
    // with a value of 'Green'
    MyColor clr = Green;

    // a Test object named 'tst'
    Test tst;

    // a variable named 'value' of type 'Test::TestEnum'
    // with a value of 'Test::value2'
    Test::TestEnum value = Test::value2;

    // assign the value 'Test::value1' to the
    // 'enumVar' member of 'tst'
    tst.enumVar = Test::value1;

    return 0;
    }


    Regards,
    Larry

    --
    Anti-spam address, change each 'X' to '.' to reply directly.
    Larry I Smith, May 21, 2005
    #2
    1. Advertising

  3. Larry I Smith wrote:
    > Teddy wrote:
    >>what is the special meaning of enum members in class ?
    >>

    >
    > An example:
    >
    > --- en.cpp ---
    >
    > // a global enum named MyColor
    > enum MyColor { Red, Green, Blue };
    >
    > // a simple class with an enum and a variable of that
    > // enum type
    > class Test
    > {
    > public:
    > enum TestEnum
    > {
    > value1,
    > value2
    > };
    >


    // a constructor to complete the example.
    // sets 'enumVar' to 'value2' when a
    // Test object is created
    Test() : enumVar(value2) {}

    > TestEnum enumVar;
    > };
    >
    > int main()
    > {
    > // a variable named 'clr' of type 'MyColor'
    > // with a value of 'Green'
    > MyColor clr = Green;
    >
    > // a Test object named 'tst'
    > Test tst;
    >
    > // a variable named 'value' of type 'Test::TestEnum'
    > // with a value of 'Test::value2'
    > Test::TestEnum value = Test::value2;
    >
    > // assign the value 'Test::value1' to the
    > // 'enumVar' member of 'tst'
    > tst.enumVar = Test::value1;
    >
    > return 0;
    > }
    >
    >
    > Regards,
    > Larry
    >


    To complete the example, I added a default constructor above.
    The example now demonstrates most of the rules for using
    global and class enums.

    Regards,
    Larry

    --
    Anti-spam address, change each 'X' to '.' to reply directly.
    Larry I Smith, May 21, 2005
    #3
  4. Teddy

    Rolf Magnus Guest

    Teddy wrote:

    > what is the special meaning of enum members in class ?


    There is no special meaning. It means the same as an enum outside any class.
    Rolf Magnus, May 21, 2005
    #4
    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:
    519
  2. Jerminia
    Replies:
    3
    Views:
    608
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    436
    Rolf Magnus
    Nov 13, 2003
  4. Brian
    Replies:
    4
    Views:
    2,574
    Brian
    Feb 27, 2010
  5. Ansel

    "enum" vs. "enum class"

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

Share This Page