typedef enum

Discussion in 'C++' started by wongjoekmeu@yahoo.com, Apr 12, 2008.

  1. Guest

    Hello all,
    I was wondering what the differences are between these two things:

    1) enum color { RED, BLUE };

    and

    2) typedef enum { RED, BLUE } color;

    Is there a difference anyway ? Why would you prefer one above the
    other ?

    Thanks for answering.

    RR
    , Apr 12, 2008
    #1
    1. Advertising

  2. asterisc Guest

    On Apr 12, 3:26 pm, "" <>
    wrote:
    > Hello all,
    > I was wondering what the differences are between these two things:
    >
    > 1) enum color { RED, BLUE };
    >
    > and
    >
    > 2) typedef enum { RED, BLUE } color;
    >
    > Is there a difference anyway ? Why would you prefer one above the
    > other ?
    >
    > Thanks for answering.
    >
    > RR


    In C++ there is no difference.
    asterisc, Apr 12, 2008
    #2
    1. Advertising

  3. Rolf Magnus Guest

    wrote:

    > Hello all,
    > I was wondering what the differences are between these two things:
    >
    > 1) enum color { RED, BLUE };


    This defines an enumerated type called "color".

    > and
    >
    > 2) typedef enum { RED, BLUE } color;


    This defines a nameless enumerated type and gives it the "alias"
    name "color".

    > Is there a difference anyway ? Why would you prefer one above the
    > other ?


    I prefer 1), because it's simpler and shorter. 2) is not needed in C++. It
    is used in C, because there, enums have a different namespace. You'd have
    to always refer to the type as "enum color" if you use 1), while you can
    simply refer to it as "color" when using 2). In C++, you can refer to it
    as "color" both ways.
    Rolf Magnus, Apr 12, 2008
    #3
  4. James Kanze Guest

    On 12 avr, 17:14, Rolf Magnus <> wrote:
    > wrote:
    > > Hello all,
    > > I was wondering what the differences are between these two things:


    > > 1) enum color { RED, BLUE };


    > This defines an enumerated type called "color".


    > > and


    > > 2) typedef enum { RED, BLUE } color;


    > This defines a nameless enumerated type and gives it the
    > "alias" name "color".


    No. There is a special rule in this case (for compatibility
    with frequent C use, and support of such use in mixed language
    headers) which says that for most intents and purposes, the
    "nameless enum type" behaves as if it had the first name in the
    typedef.

    It's obviously a compatibility hack, and I wouldn't use it in
    C++, but it is part of the language. Seeing it, however,
    strongly suggests to me that the programmer came from C, and
    hasn't yet mastered C++.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 13, 2008
    #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. qazmlp

    typedef enum

    qazmlp, Jul 2, 2003, in forum: C++
    Replies:
    2
    Views:
    447
    Alexander Terekhov
    Jul 2, 2003
  2. Fao
    Replies:
    6
    Views:
    536
    gooch
    Jan 27, 2005
  3. Chris
    Replies:
    4
    Views:
    15,697
    Ben Pfaff
    May 15, 2004
  4. Martin

    typedef declares object / enum to int

    Martin, Feb 23, 2005, in forum: C Programming
    Replies:
    13
    Views:
    1,598
    Richard Bos
    Feb 24, 2005
  5. oor
    Replies:
    0
    Views:
    1,324
Loading...

Share This Page