enum question...

Discussion in 'C++' started by Chris Mantoulidis, Dec 14, 2003.

  1. I can use enums in three ways:

    typedef enum { a, b, c, d } ABCD; //this is a TYPE
    enum EFGH { e, f, g, h }; //this is a TYPE too
    enum { i, j, k, l } IJKL; //this however is a variable...

    I believe this happens with structs too, but that's not the point.

    Why are there two ways of declaring an enum type??? Isn't one way
    enough? Or is the second way more C++-ish?

    Thanks in advance,
    cmad
    Chris Mantoulidis, Dec 14, 2003
    #1
    1. Advertising

  2. Chris Mantoulidis

    Ron Natalie Guest

    "Chris Mantoulidis" <> wrote in message
    news:...

    > Why are there two ways of declaring an enum type??? Isn't one way
    > enough? Or is the second way more C++-ish?


    They are different. The first is an alias of the unnamed enum.

    In C++ struct and enum names are themselves types names.
    In C they are not, so you either have to repeat the word enum (or
    struct) or use the typedef.
    Ron Natalie, Dec 14, 2003
    #2
    1. Advertising

  3. Chris Mantoulidis

    Rolf Magnus Guest

    Chris Mantoulidis wrote:

    > I can use enums in three ways:
    >
    > typedef enum { a, b, c, d } ABCD; //this is a TYPE
    > enum EFGH { e, f, g, h }; //this is a TYPE too
    > enum { i, j, k, l } IJKL; //this however is a variable...
    >
    > I believe this happens with structs too, but that's not the point.
    >
    > Why are there two ways of declaring an enum type???


    They are different things, though the result is the same. typedef is
    there to give an alias name to an existing type.
    The first example defines a nameless enum type and gives it an "alias"
    name of 'ABCD'. The second one creates an enum with name 'EFGH'. You
    can also write:

    enum EFGH { e, f, g, h};
    typedef EFGH ABCD;

    or even combined:

    typedef enum EFGH { e, f, g, h} ABCD;

    Both of those define an enum named EFGH and give them an alias name of
    'ABCD'.


    > Isn't one way enough?


    Well, one of them is just a side effect of the existance of typedef.
    There is often more than one way to do the same thing.
    Rolf Magnus, Dec 14, 2003
    #3
  4. Chris Mantoulidis

    jeffc Guest

    "Chris Mantoulidis" <> wrote in message
    news:...
    > I can use enums in three ways:
    >
    > typedef enum { a, b, c, d } ABCD; //this is a TYPE
    > enum EFGH { e, f, g, h }; //this is a TYPE too
    > enum { i, j, k, l } IJKL; //this however is a variable...
    >
    > I believe this happens with structs too, but that's not the point.
    >
    > Why are there two ways of declaring an enum type??? Isn't one way
    > enough? Or is the second way more C++-ish?


    To answer your question specifically, yes the second way is more C++-ish.
    jeffc, Dec 15, 2003
    #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:
    515
  2. Eric Sosman
    Replies:
    3
    Views:
    766
    Mayeul
    Feb 26, 2010
  3. Wojtek
    Replies:
    1
    Views:
    478
  4. Lew
    Replies:
    0
    Views:
    519
  5. Roedy Green
    Replies:
    0
    Views:
    713
    Roedy Green
    Feb 27, 2010
Loading...

Share This Page