Is the following syntax is correct for an enum?

给你一囗甜甜゛ 提交于 2020-03-05 03:16:32

问题


enum  {
  ValidationLoginFailed=2000,
  ValidationSessionTokenExpired=2001,
  ValidationSessionTokenInvalid=2002,
  ValidationEmailNotFound=2003
  ValidationSucccesMIN=ValidationLoginFailed,
  ValidationSucccesMAX=ValidationEmailNotFound,
  ValdationValidSuccessCode=9999,
  ValdationInvalidCode=10000

}; 
typedef int ValidationStatusCodes;

please help me out.


回答1:


In your code, ValidationStatusCodes means int, not your anonymous enum type. So they aren't actually connected in any way.

However, since your enum contains int values, you could say that there's some sort of relation. You can pass the names of the enumerated values and they will be considered of the int or ValidationStatusCodes type.

By the way, Apple does something similar to what you do, except they typedef their collective names to NSInteger or NSUInteger instead of int or uint. See this question for an example.

With all that said, a more common practice is to typedef your custom type name directly to the anonymous enum, like this:

typedef enum {
    ValidationLoginFailed = 2000,
    ValidationSessionTokenExpired = 2001,
    ValidationSessionTokenInvalid = 2002,
    ValidationEmailNotFound = 2003
    ValidationSuccessMIN = ValidationLoginFailed,
    ValidationSuccessMAX = ValidationEmailNotFound,
    ValdationValidSuccessCode = 9999,
    ValdationInvalidCode = 10000
} ValidationStatusCodes;


来源:https://stackoverflow.com/questions/5785965/is-the-following-syntax-is-correct-for-an-enum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!