Objective-C / C giving enums default values

前端 未结 2 698
南旧
南旧 2021-02-06 00:32

I read somewhere about giving enums default values like so:

typedef enum  {
MarketNavigationTypeNone = 0,
MarketNavigationTypeHeirachy = 1,
MarketNavigationTypeM         


        
2条回答
  •  清歌不尽
    2021-02-06 01:21

    That are not default values, you are giving them the values they will always have.

    If you wouldn't initialize them explicitly, the first enumerators value is zero. For all others, if there is no initializer, their value is the value of the previous enumerator increased by one.

    There are two reasons for giving them explicit values:

    • you don't want them to have the values they'd have otherwise
    • you want to make it clear what value they have (for you or other developers)

    If you always refer to them by their name and never explicitly use an integral value for comparison or assignment, explicitly giving them a value is not needed.

提交回复
热议问题