How to: Specify Underlying Types of Enums

By default, the underlying type of an enumeration is int. However, you can specify the type to be signed or unsigned forms of int, short, long, __int32, or __int64. You can also use char or bool.

Example

Code

// mcppv2_enum_3.cpp
// compile with: /clr
typedef bool MYBOOL;

public enum class day_bool : MYBOOL {sun = true, mon = false, tue = true, wed = false, thu = true, fri = false, sat = true};
public enum class day_char : char {sun, mon, tue, wed, thu, fri, sat};

int main() {
   // fully qualified names, enumerator not injected into scope
   day_bool a = day_bool::sun, b = day_bool::mon;   

   System::Console::WriteLine(a);
   bool c = (bool)a;
   System::Console::WriteLine(c);
   c = (bool)b;
   System::Console::WriteLine(c);

   day_char d = day_char::sun, e = day_char::mon;
   System::Console::WriteLine(d);
   char f = (char)d;
   System::Console::WriteLine(f);
   f = (char)e;
   System::Console::WriteLine(f);
   e = day_char::tue;
   f = (char)e;
   System::Console::WriteLine(f);
}

Output

True
True
False
sun
0
1
2

See Also

Reference

enum class