简体   繁体   English

你能解释一下这个枚举声明吗?

[英]Can you explain this enum declaration?

I was reading the code from a JsonParser and I am wondering how this enum works: 我正在从JsonParser读取代码,并且想知道此枚举如何工作:

enum JsonTag {
    JSON_NUMBER = 0,
    JSON_STRING,
    JSON_ARRAY,
    JSON_OBJECT,
    JSON_TRUE,
    JSON_FALSE,
    JSON_NULL = 0xF
};
  • Does this mean that NULL is defined as a half byte 1111 ? 这是否意味着将NULL定义为半字节1111

  • Does this mean that every value in between NUMBER and NULL will be between 0 and 0xF ? 这是否意味着NUMBERNULL之间的每个值都将在00xF之间?

  • Does this mean NULL is some kind of memory location? 这是否意味着NULL是某种内存位置?

  • What would the value of STRING , ARRAY ... be? STRINGARRAY的值是什么?

  • Why declare an enum this way? 为什么用这种方式声明一个枚举?

It is very simple. 这很简单。 Enum values can be either explicitly assigned or implicitly defined. 枚举值可以显式分配或隐式定义。 When assigned explicitly - like JSON_NULL in your example - they simply have this value. 当显式分配时(如您的示例中的JSON_NULL ,它们仅具有此值。 In this case it is 15. (Not sure why fancy hex notation is used here). 在这种情况下,它是15。(不确定为什么在这里使用花哨十六进制表示法)。

When assigned implicitly, they are always equal to previous enum value + 1, the very first value being set to 0. 隐式分配时,它们始终等于先前的枚举值+ 1,第一个值设置为0。

And, just in case, the full names are defined there. 并且,以防万一,全名在那里定义。 Not NULL , but JSON_NULL , not NUMBER , but JSON_NUMBER , etc. 不是NULL ,而是JSON_NULL ,不是NUMBER ,但是JSON_NUMBER等。

Does this mean that JSON_NULL is defined as a half byte 1111? 这是否意味着将JSON_NULL定义为半字节1111?

The value 0x0F (hexadecimal) is 15 in decimal and 1111 in binary. 值0x0F(十六进制)为十进制的15和二进制的1111。

Does this mean that every value in between JSON_NUMBER and JSON_NULL will be between 0 and 0xF? 这是否意味着JSON_NUMBER和JSON_NULL之间的每个值都将介于0和0xF之间?

Not necessarily. 不必要。 Assigned enum values have nothing to do with ranges. 分配的enum值与范围无关。
When an enum identifier does not have a specific value, it will use the previous value + 1. enum标识符没有特定值时,它将使用先前的值+ 1。

Does this mean JSON_NULL is some kind of memory location? 这是否意味着JSON_NULL是某种内存位置?

The only meaning you can get is that the identifier JSON_NULL is assigned the value 15. There is nothing stating the purpose of any of the identifiers. 您可以获得的唯一含义是为标识符JSON_NULL分配了值15。没有任何内容说明任何标识符的用途。

What would the value of JSON_STRING, JSON_ARRAY... be? JSON_STRING,JSON_ARRAY ...的值是什么?

Here are the values of the identifiers: 这是标识符的值:

enum JsonTag {
/* 0 */    JSON_NUMBER = 0,
/* 1 */    JSON_STRING,
/* 2 */    JSON_ARRAY,
/* 3 */    JSON_OBJECT,
/* 4 */    JSON_TRUE,
/* 5 */    JSON_FALSE,
/* 15 */    JSON_NULL = 0xF
};

Why declare an enum this way? 为什么用这种方式声明一个枚举?

One reason is to have sequential symbols, and allow for a symbol that is not sequential. 原因之一是具有顺序符号,并允许使用非顺序符号。 Perhaps the author doesn't have 15 symbols defined, but only the first 6. The JSON_NULL seems to have a special value of 15, so it is listed that way. 也许作者没有定义15个符号,而仅定义了6个JSON_NULL似乎具有15的特殊值,因此以这种方式列出。

You may want to explore the JSON object types in the json data specification. 您可能想探索json数据规范中的JSON对象类型。

0xF is just 15 (decimal) in hexadecimal notation - nothing special about it. 0xF只是十六进制表示的15(十进制),对此没有什么特别的。 Enum entries either have the value explicitly assigned or the value of the previous entry plus one. 枚举条目具有显式分配的值或上一个条目的值加一个。 There's nothing strange here, the enum just has 6 values numbered 0 through 5 and then a final value numbered 15. 7 values in total, numbered 0, 1, 2, 3, 4, 5, 15. Why this numbering? 这里没有什么奇怪的,枚举只有6个值,编号从0到5,然后是最终值,编号为15。总共有7个值,编号为0、1、2、3、4、5、15。为什么要这样编号? I don't know; 我不知道; ask whomever wrote the code. 询问编写代码的人。

  • Does this mean that NULL is defined as a half byte 1111? 这是否意味着将NULL定义为半字节1111?

No, in general, unless specified explicitly the underlying enum type is an int. 否,通常,除非明确指定,否则基本枚举类型为int。 The value in this case is simply 0xF - no half bytes there. 在这种情况下,该值仅为0xF-那里没有半字节。

  • Does this mean that every value in between NUMBER and NULL will be between 0 and 0xF? 这是否意味着NUMBER和NULL之间的每个值都将介于0和0xF之间?

It will start from JSON_NUMBER=0 and increase by one until JSON_FALSE, then for JSON_NULL it will be 0xF 它将从JSON_NUMBER = 0开始并增加一个直到JSON_FALSE,然后对于JSON_NULL它将是0xF

  • Does this mean NULL is some kind of memory location? 这是否意味着NULL是某种内存位置?

No. 没有。

  • What would the value of STRING, ARRAY... be? STRING,ARRAY ...的价值是多少?

As described previously. 如前所述。

  • Why declare an enum this way? 为什么用这种方式声明一个枚举?

Typically to have a value which can be used as undefined or invalid . 通常具有一个可以用作undefinedinvalid It can denote that something went wrong when executing a function so the enum value was not set to a valid entry. 它可以表示执行函数时出了点问题,因此enum值未设置为有效条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM