Partager via


enum class 

Declares a managed enumeration. An enumeration is a user-defined type consisting of a set of named constants called enumerators.

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

Parameters

  • access
    The accessibility of the enum. Can be either public or private.
  • enumerator-list
    A comma-separated list of the identifiers (enumerators) in the enumeration.
  • name
    The name of the enumeration. Anonymous managed enumerations are not allowed.
  • type (optional)
    The underlying type of the identifiers. This can be any scalar type, such as signed or unsigned versions of int, short, or long. bool or char is also allowed.
  • var (optional)
    The name of a variable of the enumeration type.

Remarks

enum class and enum struct are equivalent declarations.

There are two types of enums: managed and standard.

A managed enum might be defined as follows,

enum class day {sun, mon };

and is semantically equivalent to:

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

A standard enum might be defined as follows:

enum day2 {sun, mon, };

and is semantically equivalent to:

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

Managed enumerator names (identifiers) are not injected into the scope where the enumeration is defined; all references to the enumerators must be fully qualified (name**::**identifier). For this reason, you cannot define an anonymous managed enum.

The enumerators of a standard enum are strongly injected into the enclosing scope. That is, if there is another symbol with the same name as an enumerator in the enclosing scope, the compiler will generate an error.

In Visual C++ 2002 and Visual C++ 2003, enumerators were weakly injected (visible in the enclosing scope unless there was another identifier with the same name).

If a standard C++ enum is defined (without class or struct), compiling with /clr will cause the enumeration to be compiled as a managed enum. The enumeration still has the semantics of an unmanaged enumeration. Note, the compiler injects an attribute, Microsoft::VisualC::NativeEnumAttribute, which the Visual C++ compiler recognizes, to identify a programmer's intent for the enum to be a native enum. Other compilers will simply see the standard enum as a managed enum.

A named, standard enum compiled with /clr will be visible in the assembly as a managed enum, and can be consumed by any other managed compiler. However, an unnamed standard enum will not be publicly visible from the assembly.

In Visual C++ 2002 and Visual C++ 2003, a standard enum used as the type in a function parameter:

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

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

would emit the following in MSIL for the function signature:

void f(int32);

However, in current versions of the compiler, the standard enum is emitted as a managed enum with a [NativeEnumAttribute] and the following in MSIL for the function signature:

void f(E)

For more information about native enums, see C++ Enumeration Declarations.

In the development environment, you can get F1 help on these keywords by highlighting the keyword, (enum class, for example) and pressing F1.

For more information on CLR enums, see:

Example

// 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);
}

Output

no automatic conversion to int: b
convert to int: 1
1
1

See Also

Concepts

New C++ Language Features