枚 举 2.014

[size=large]枚举声明:
enum 枚举标记 枚举体
enum 枚举体
enum 枚举标记 :枚举基类型 枚举体
enum :枚举基类型 枚举体

枚举标记:
标识符

枚举基类型:
类型

枚举体:
;
{ 多个枚举成员 }

多个枚举成员:
单个枚举成员
单个枚举成员 ,
单个枚举成员 , 多个枚举成员

单个枚举成员:
标识符
标识符 = 赋值表达式
[color=red]Type Identifier = AssignExpression 2.014[/color]

枚举声明被用来定义一组常量。 定义形式有两种:
1. 命名枚举——它有一个枚举标记。
2. 匿名枚举——它没有枚举标记。


14.1 命名枚举

命名枚举常被用来声明几个有关联的常量,并且使用一个统一的类型来组合它们。 多个枚
举成员 被声明在枚举标记的作用域内。 枚举标记 会声明出一个新的类型,而所有的那些
枚举成员 都属于该类型。

下面的例子就定义了一种新的类型 X ——它拥有值 X.A=0、X.B=1、X.C=2:
enum X { A, B, C } // 命名枚举

如果 枚举基类型 没有显示地设置,则它会被设置成 int 类型。

上面这句在2.014中被改成下面的:
[color=red]If the EnumBaseType is not explicitly set, and the first EnumMember has an initializer, it is set to the type of that initializer. Otherwise, it defaults to type int. 2.014[/color]

命名枚举成员可以被隐式转换成它的 枚举基类型,不过 枚举基类型 不会被隐式转换成枚
举类型。

一个 枚举成员值 由初始化过程设定。 如果没有初始化过程,则它会被设定成前一个 枚举
成员 的值 + 1。如果它是第一个 枚举成员,那么它的值就为 0。

枚举必须至少拥有一个成员。


1.1 枚举默认的初始化过程

枚举类型的特性 .init 就是该枚举的第一个成员的值。 它也是枚举类型的默认初始化过
程。
enum X { A=3, B, C }
X x; // ax 被初始化为 3


1.2 枚举的特性

枚举特性仅对命令枚举才有效。
.init 第一个枚举成员的值
.min 枚举的最小值
.max 枚举的最大值
.sizeof 存储枚举值所需要的存储器大小

例如:
enum X { A=3, B, C }
X.min // 为 X.A
X.max // 为 X.C
X.sizeof // 等同于 int.sizeof

[color=red]The EnumBaseType of named enums must support comparison in order to compute the .max and .min properties. 2.014[/color]


2 匿名枚举

如果不指定 enum 标识符,则枚举称为 匿名枚举,并且 多个枚举成员 在 枚举声明 出现
的作用域内被声明。没有新类型被创建;多个枚举成员 的类型是 枚举基类型。

枚举基类型 是枚举的基本类型。

它必须是整数类型。如果忽略,默认为 int。

[color=red]2.014中上面这句被改成如下:
If omitted, the EnumMembers can have different types. Those types are given by the first of:

The Type, if present.
The type of the AssignExpression, if present.
The type of the previous EnumMember, if present.
int [/color]


enum { A, B, C } // 匿名枚举
定义了常量 A=0、B=1、C=2,所有类型都为 int。

枚举必须至少拥有一个成员。

一个 枚举成员值 由初始化过程设定。 如果没有初始化过程,则它会被设定成前一个 枚举
成员 的值 + 1。如果它是第一个 枚举成员,那么它的值就为 0。

enum { A, B = 5+7, C, D = 8+C, E }
设置 A=0、B=12、C=13、D=21 和 E=22,所有的类型都为 int。

enum : long { A = 3, B }
设置 A=3、B=4,所有类型都为 long。


[color=red]2.014
enum : string
{
A = "hello",
B = "betty",
C // error, cannot add 1 to "betty"
}
enum
{
A = 1.2f, // A is 1.2f of type float
B, // B is 2.2f of type float
int C = 3, // C is 3 of type int
D // D is 4 of type int
}
Manifest Constants
If there is only one member of an anonymous enum, the { } can be omitted:

enum i = 4; // i is 4 of type int
enum long l = 3; // l is 3 of type long
Such declarations are not lvalues, meaning their address cannot be taken.
[/color]


[/size]


............
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值