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

后端 未结 9 835
别那么骄傲
别那么骄傲 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: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.

提交回复
热议问题