enum 类(C++ 组件扩展)

声明枚举,是用户定义的类型包括的设置命名常量调用枚举数。

所有运行时

备注

C++/CX 和 C++/CLI 支持新的标准 C++ 关键字, enum class。 有关旧标准 C++ 关键字的更多信息, enum,请参见 C++枚举声明

Windows 运行时

语法

access enum class enumeration-identifier [:underlying-type] { enumerator-list } [var];
access enum struct enumeration-identifier [:underlying-type] { enumerator-list } [var];

参数

  • 访问
    枚举的辅助功能,可以是 public 或 private。

  • 枚举标识符
    枚举的名称。

  • 基础类型
    (可选) 枚举的基础类型。

    (可选) 枚举的基础类型,可以是 bool、 char、 char16、 int16、 uint16、 int、 uint32、 int64或 uint64。

  • 枚举数列表
    逗号分隔列表枚举数名称。

    每个枚举数的值为或为隐式由编译器定义的常数表达式,或者显式由表示形式, 枚举数=常数表达式。 默认情况下,因此,如果它隐式定义,第一个数的值为零。 每个后续 implictly 定义的枚举数的值是枚举的枚举数的值 + 1。

  • var
    (可选) 枚举类型的变量的名称。

备注

请注意编译器发出错误消息,如果定义了枚举数的值的常数表达式不能由 基础类型表示。 但是,编译器会报告为基础类型不对应的值的错误。 例如:

  • 如果 基础类型 是数字的,因此,枚举数为该类型指定最大值,下隐式定义的 enumeratoin 的值不能表示。

  • 如果 基础类型 是 bool,因此,两个以上的枚举数隐式定义,枚举数,在前两个不能表示之后。

  • 如果 基础类型 是 char16,并且,枚举值从 0xD800 到 0xDFFF,值可以表示。 但是,逻辑上不正确中的值,因为它表示一半 Unicode 代理项对不应出现在隔离。

a6cskb49.collapse_all(zh-cn,VS.110).gif要求

编译器选项: /ZW

公共语言运行时

语法

access enum class name [:type] { enumerator-list } var;
access enum struct name [:type] { enumerator-list } var;

参数

  • access
    枚举的可访问性。 可以是 公共 或 private。

  • enumerator-list
    逗号分隔列表标识符 () 枚举数在枚举。

  • name
    枚举的名称。 匿名托管枚举不允许的。

  • type(可选)
    标识符的基础类型。 它可以是任何数据类型,如 int 签名的或未签名的版本,短或 long。 bool 或 char 还允许。

  • var(可选)
    枚举类型的变量的名称。

备注

enum classenum struct 等效的说明。

具有枚举的两种类型:管理和条件。

托管枚举可能如下定义,

enum class day {sun, mon };

和语义上等效于:

ref class day {
public:
   static const int sun = 0;
   static const int mon = 1;
};

标准枚举来定义如下:

enum day2 {sun, mon, };

和语义上等效于:

static const int sun = 0;
static const int mon = 1;

托管枚举数名称 (标识符) 未被插入到枚举中定义的大小;所有对枚举数必须是完全限定的名称(::标识符)。 因此,不能定义匿名托管枚举。

标准枚举的枚举数强插入到封闭范围。 也就是说,如果有名称的其他符号和封闭范围的枚举数相同,编译器将生成错误。

在 Visual C++ 2002 和 Visual C++ 2003 中,弱插入了枚举数 (显示在封闭范围,除非存在同名的另一个标识符)。

如果标准 C++ 枚举定义 (不 class 或 struct),用 /clr 编译将导致枚举编译为托管枚举。 枚举仍具有非托管枚举的语义。 请注意,编译器插入属性, Microsoft::VisualC::NativeEnumAttribute, Visual C++ 编译器识别,标识枚举的程序员的意图可以是本机枚举。 其他编译器将看到标准枚举作为托管枚举。

命名,标准枚举用 /clr 编译会显示在程序集作为托管枚举,可由其他托管编译器使用。 但是,未命名的标准枚举从程序集不是公开可见的。

在 Visual C++ 2002 和 Visual C++ 2003,作为类型的标准枚举在函数参数:

// mcppv2_enum.cpp
// compile with: /clr
enum E { a, b };
void f(E) {System::Console::WriteLine("hi");}

int main() {
   E myi = b;
   f(myi);
}

将发出以下函数签名的 MSIL:

void f(int32);

但是,在编译器的最新版本,标准枚举发出作为具有 [] NativeEnumAttribute 和以下托管枚举在函数签名的 MSIL:

void f(E)

有关本机枚举的更多信息,请参见 C++ 枚举声明

在开发环境中,您通过显示关键字,enum class(例如,) 并按 F1 可以在这些关键字获取 F1 帮助。

有关 CLR 枚举的更多信息,请参见:

a6cskb49.collapse_all(zh-cn,VS.110).gif要求

编译器选项: /clr

a6cskb49.collapse_all(zh-cn,VS.110).gif示例

示例

desc

// mcppv2_enum_2.cpp
// compile with: /clr
// managed enum
public enum class m { a, b };

// standard enum
public enum n { c, d };

// unnamed, standard enum
public enum { e, f } o;

int main() {
   // consume managed enum
   m mym = m::b;
   System::Console::WriteLine("no automatic conversion to int: {0}", mym);
   System::Console::WriteLine("convert to int: {0}", (int)mym);

   // consume standard enum
   n myn = d;
   System::Console::WriteLine(myn);

   // consume standard, unnamed enum
   o = f;
   System::Console::WriteLine(o);
}Optionally, copy this sequence of tags for each example you want, then populate the text nodes. That is, after section+title("Examples")+content, insert: para+embeddedLabel ("Example")+para(sample description); code(sample); para+embeddedLabel("Output"); computerOutput(sample output, including 'br' tag instead of newlines)

Output

  
  
  
  

请参见

概念

适用于运行时平台的组件扩展