GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — enum Class in C++
Selected Reading
enum Class in C++An enumeration, or enum in C++, is a user-defined data type formed by a set of named integral constants. However, enum types from tradition have the disadvantage of potential name clashes and the absence of type safety. To solve this problem, C++ introduced the concept of enum class, which is also referred to as scoped enumerations. Declaring an enum ClassThis is the following syntax of declaring an enum class, which is done by using scoped enumeration (enum class). enum class EnumName {
Value1,
Value2,
Value3,
// ... other enumerator values.
};
Here, EnumName is the name of the enumeration class. Value1, Value2.. is the named value(constants) inside the enum class. Accessing Values from an enum ClassYou can access the values of an enum class by using the Scope Resolution Operator (::) with the enum class name. Consider the following syntax: EnumName::Value Here, EnumName is the name of the enum class. Value is one of the enumerators defined in the class. Example of Enum ClassHere is the following example for the enum class in C++. #include <iostream>
using namespace std;
// Declare an enum class for days of the week
enum class Day {
Monday, // 0
Tuesday, // 1
Wednesday, // 2
Thursday, // 3
Friday, // 4
Saturday, // 5
Sunday // 6
};
int main() {
Day today = Day::Friday;
if (today == Day::Friday) {
cout << "Today is Friday!" << endl;
}
return 0;
}
OutputToday is Friday! Explanation
Underlying Type of an Enum ClassThe enum class uses an integer as its default underlying type but many more underlying types are possible: unsigned int, char, uint8_t, int16_t, etc. Default Underlying Type of enum classWhen you declare the enum class without specifying any underlying type, then by default it is set to int. This means that each enumerator's value will be stored as an int (4 bytes). As we already see in the above following example. Specifying a Custom Underlying Type for an enum classHere, you can explicitly specify the underlying type of an enum class by adding a colon (:) after the enum class name. This will allow for control of the storage size and range of values. SyntaxHere is the following syntax for it. enum class EnumName : UnderlyingType {
Value1,
Value2,
Value3
};
Here, UnderlyingType could be any integral type (like unsigned int, char, uint8_t, etc.). Example of Underlying Type of an Enum ClassHere is the following example using unsigned int as Underlying Type. #include <iostream>
using namespace std;
enum class Status : unsigned int {
Ok = 0, // 0
Error = 1, // 1
Warning = 2 // 2
};
int main() {
Status s = Status::Error;
// Print the integer value of the enum
cout << "The integer value of Error is: " << static_cast<unsigned int>(s) << endl;
return 0;
}
OutputThe integer value of Error is: 1 Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |