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

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

提交回复
热议问题