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

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

    Holdover from C.

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

    The actual answer to the "why" question (which is surprisingly ignored by the existing answers top this old question) is that this enum declaration is probably located in a header file that is intended to be cross-compilable as both C and C++ code (i.e. included into both C and C++ implementation fiules). The art of writing such header files relies on the author's ability to select language features that have the proper compatible meaning in both languages.

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

    In some C codestyle guide the typedef version is said to be preferred for "clarity" and "simplicity". I disagree, because the typedef obfuscates the real nature of the declared object. In fact, I don't use typedefs because when declaring a C variable I want to be clear about what the object actually is. This choice helps myself to remember faster what an old piece of code actually does, and will help others when maintaining the code in the future.

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