什么是枚举变量的缺省值 [英] what is the default value for enum variable

查看:551
本文介绍了什么是枚举变量的缺省值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是枚举变量,任何人都知道,如果它总是默认为第一个元素?

An enum variable, anyone know if it is always defaulting to the first element?

推荐答案

这是什么枚举重$ P $成员psents值 0 。具体而言,从文档

It is whatever member of the enumeration represents the value 0. Specifically, from the documentation:

枚举è的默认值为由前pression产生的值(E)0

作为一个例子,采取以下枚举:

As an example, take the following enum:

enum E
{
    Foo, Bar, Baz, Quux
}

在不覆盖缺省值,打印默认(E)返回,因为它是第一个存在的元素。

Without overriding the default values, printing default(E) returns Foo since it's the first-occurring element.

然而,这是的不一定的一个枚举的 0 被重新$ P的第一个成员psented $。例如,如果你这样做:

However, it is not always the case that 0 of an enum is represented by the first member. For example, if you do this:

enum F
{
    // Give each element a custom value
    Foo = 1, Bar = 2, Baz = 3, Quux = 0
}

印刷默认(F)会给你 QUUX ,不是

如果没有任何元素的枚举对应 0

If none of the elements in an enum G correspond to 0:

enum G
{
    Foo = 1, Bar = 2, Baz = 3, Quux = 4
}

默认(G)返回字面上 0 ,虽然它的类型仍然是(引述上面的文档,一个投给定的枚举类型)。

default(G) returns literally 0, although its type remains as G (as quoted by the docs above, a cast to the given enum type).

这篇关于什么是枚举变量的缺省值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆