Why do you use typedef when declaring an enum in C++?

后端 未结 9 806
别那么骄傲
别那么骄傲 2020-11-30 17:39

I haven\'t written any C++ in years and now I\'m trying to get back into it. I then ran across this and thought about giving up:

typedef enum TokenType
{
            


        
相关标签:
9条回答
  • 2020-11-30 18:02

    Some people say C doesn't have namespaces but that is not technically correct. It has three:

    1. Tags (enum, union, and struct)
    2. Labels
    3. (everything else)

    typedef enum { } XYZ; declares an anonymous enumeration and imports it into the global namespace with the name XYZ.

    typedef enum ABC { } XYZ; declares an enum named ABC in the tag namespace, then imports it into the global namespace as XYZ.

    Some people don't want to bother with the separate namespaces so they typedef everything. Others never typedef because they want the namespacing.

    0 讨论(0)
  • 2020-11-30 18:04

    In C, declaring your enum the first way allows you to use it like so:

    TokenType my_type;
    

    If you use the second style, you'll be forced to declare your variable like this:

    enum TokenType my_type;
    

    As mentioned by others, this doesn't make a difference in C++. My guess is that either the person who wrote this is a C programmer at heart, or you're compiling C code as C++. Either way, it won't affect the behaviour of your code.

    0 讨论(0)
  • 2020-11-30 18:06

    It's a C heritage, in C, if you do :

    enum TokenType
    {
        blah1   = 0x00000000,
        blah2   = 0X01000000,
        blah3   = 0X02000000
    };
    

    you'll have to use it doing something like :

    enum TokenType foo;
    

    But if you do this :

    typedef enum e_TokenType
    {
        blah1   = 0x00000000,
        blah2   = 0X01000000,
        blah3   = 0X02000000
    } TokenType;
    

    You'll be able to declare :

    TokenType foo;
    

    But in C++, you can use only the former definition and use it as if it were in a C typedef.

    0 讨论(0)
  • 2020-11-30 18:17

    You do not need to do it. In C (not C++) you were required to use enum Enumname to refer to a data element of the enumerated type. To simplify it you were allowed to typedef it to a single name data type.

    typedef enum MyEnum { 
      //...
    } MyEnum;
    

    allowed functions taking a parameter of the enum to be defined as

    void f( MyEnum x )
    

    instead of the longer

    void f( enum MyEnum x )
    

    Note that the name of the typename does not need to be equal to the name of the enum. The same happens with structs.

    In C++, on the other hand, it is not required, as enums, classes and structs can be accessed directly as types by their names.

    // C++
    enum MyEnum {
       // ...
    };
    void f( MyEnum x ); // Correct C++, Error in C
    
    0 讨论(0)
  • 2020-11-30 18:17

    This is kind of old, but anyway, I hope you'll appreciate the link that I am about to type as I appreciated it when I came across it earlier this year.

    Here it is. I should quote the explanation that is always in my mind when I have to grasp some nasty typedefs:

    In variable declarations, the introduced names are instances of the corresponding types. [...] However, when the typedef keyword precedes the declaration, the introduced names are aliases of the corresponding types

    As many people previously said, there is no need to use typedefs declaring enums in C++. But that's the explanation of the typedef's syntax! I hope it helps (Probably not OP, since it's been almost 10 years, but anyone that is struggling to understand these kind of things).

    0 讨论(0)
  • 2020-11-30 18:20

    In C, it is good style because you can change the type to something besides an enum.

    typedef enum e_TokenType
    {
        blah1   = 0x00000000,
        blah2   = 0X01000000,
        blah3   = 0X02000000
    } TokenType;
    
    foo(enum e_TokenType token);  /* this can only be passed as an enum */
    
    foo(TokenType token); /* TokenType can be defined to something else later
                             without changing this declaration */
    

    In C++ you can define the enum so that it will compile as C++ or C.

    0 讨论(0)
提交回复
热议问题