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

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

提交回复
热议问题