Hello, C + + (13) The answer to this single-choice question is a, B, C or d? 3.7 Enum Types

Source: Internet
Author: User

3.7 Enum Types

In addition to the numerical data and text data we introduced before, in the real world, often encounter such a kind of data: a single choice of answer can only be a, B, C, D four one of the options; the color of the traffic lights can only be one of the red, green and yellow; a person's sex can only be male or female. This data has only a limited number of possible values, and its value can only be one of the range. To abstract and express this special data, C + + provides an enumeration mechanism.

Using the enumeration mechanism of C + +, we can define a new data type by enumerating all possible values of a data type, which is often referred to as an enumeration type. Conversely, when you use an enumeration type to define a variable, the value of the variable is limited to the range of possible values enumerated by the enumeration type. The syntax format for defining an enumeration type is as follows:

enum enumeration type name {    //  Possible enumeration value list //  Note that this must end with a semicolon

Where an enum is a keyword that defines an enumeration type, the enumeration type name is the name of the new data type to be created, and when the enumeration type is defined, we can use it as the data type to define the variable, and in the definition of the enumeration type, you can list all possible values of the enumeration type individually. For example, you can abstract data that describes the color of a traffic light into an enumeration type:

// Traffic light Color enum trafficlight{Red    ,     //  red    Gerrn,/  /  green    YELLOW  //  yellow };

With this definition of the TrafficLight enumeration type, we can use it as a data type to define a variable that represents the color of the traffic light:

// Define a variable light indicates the color of the traffic lights // assigns the enumeration value red to it, indicating that the current traffic light is red TrafficLight light = RED;

Because of the specificity of the data expressed by enumeration types, we need to be aware of the following points when applying enumeration types.

1. Enumeration values in Enum types have a corresponding default integer value

In essence, the enumeration type data is an integer value, and the optional value of each enumeration type is actually an integer. By default, the first enumeration value corresponds to an integer value of 0, the second is 1, and so on. For example, the red optional value in the TrafficLight enumeration type above is actually an integer value of 0, and Green is 1,yellow natural is 2. If you think that the default corresponding integer value for the enumeration values is inappropriate. For example, in some cases, we want an enumeration value to have a special integer value to represent a special meaning, or you can specify an integer value corresponding to each enumeration value at the time of definition. For example:

// Specify the integer value corresponding to each enumeration value separately enum trafficlight{    1//  Specify the corresponding integer value, no longer starting from 0    Gerrn,  //  2     0   //  specifies that the corresponding integer value is 0, indicating a special meaning };

After the value of the enumeration is artificially specified, the value of red corresponds to 1, and then the Green adds a natural to 2, and for the final yellow, because of its special meaning, we artificially designate it as 0.

2. Assigning values to enumerated type variables

If the variable is an enumeration type, it can only be assigned using an enumeration value of this enumeration type. For example:

// Red light TrafficLight light = RED; // turn green light = Green; // If you assign a value to data other than the value of the enumeration, it causes a compilation error // even if this value is an integer value corresponding to an enumeration value 1;

In other words, if you want to limit the value of a variable to a range of optional values, you can define the variable as an enumeration type variable.

3. The enumeration value is a constant, and the corresponding integer value cannot be changed after the definition

When defining an enumeration type, the definition of the enumeration value is completed, and the corresponding value is not the default value, which is the given special value. After the definition is complete, the values of each enumeration value become constants, which are treated as constants and cannot be assigned a value. That is, you cannot change the integer value corresponding to one of the enumeration values after the definition is complete. For example, the following statement is illegal:

4;  // attempt to change the enumeration value, resulting in a compilation error

The enumeration value in the enumeration type is essentially some integral type constant. In the example above, it is possible to use 3 integer constants to represent the three colors of a traffic light. However, enumeration types allow us to use descriptive names to represent integer values in a finite range, rather than using ambiguous integer values directly, which helps ensure that the variables are given legitimate expectations, which makes our code more meaningful, readable, and easier to maintain. Therefore, if the data to be expressed "has only a limited number of possible values", we should prefer to use the enumeration type.

Know more: Enum class with scope

Although enumeration types can easily define enumeration values within a range. However, because traditional enumeration types are integers of type int in nature, there are often a variety of problems in use. For example, all the enumeration values of a traditional enumeration type can be used within the scope of the code after its definition, which can cause the name to be contaminated so that subsequent code can no longer use the enumeration value name for other purposes, and cannot specify the underlying data type of the enumeration type, which could waste memory resources. It also makes it possible for enum types to be forward-declared. The so-called forward declaration, that is, when an element (function or class) has not yet been defined, in order to use it in advance of the declaration, to the compiler to indicate that the source file has the definition of this element, now can be assured of the use of the bold, and the specific definition will be given later. To solve these problems with traditional enumeration types, C++11 proposes new scopes and enumeration classes that can specify the underlying data types.

The syntax for defining an enumeration class is similar to the syntax for defining a traditional enumeration type:

enum class Enumeration class Name: Data type {    //  Possible enumeration value list };

The definition of an enumeration class begins with "Enum class" followed by an enumeration class name followed by a colon ":" To specify the underlying data type of the enumeration class. The underlying data type of an enumeration class must be a signed or unsigned integer, and if not specified, its default data type is int. For example:

// defines an enumeration class TrafficLight, // and specifies that its underlying data type is char type enum class Char {    1//  red    Gerrn,  //  green    YELLOW  //  yellow };

Although the definition of an enumeration class is very similar to the definition of a traditional enumeration type, they are already two completely different concepts because of the differences in the intrinsic mechanisms. The enumeration class has scope and its enumeration values are visible only within its scope, which solves the name pollution problem that the enumeration value can cause. For example:

// enumeration value Red belongs to the TrafficLight scope // so we have to add trafficlight in front of it to access TrafficLight light = trafficlight::red; // defines a variable named red, although with the same name as the enumeration value red, // But there is no conflict between the two, which is the definition of the enumeration value red. // did not cause name pollution BOOL true;

In addition to resolving the name pollution problem, because the underlying data type of the enumeration can be specified, this makes the forward declaration possible, and depending on how much of the enumeration value is chosen, the appropriate underlying data type can also be avoided to some extent from waste of resources. For example:

//before the use of the forward declaration, just declare the name of the enumeration class,//no specific enumeration values are definedenum classTrafficLight:Char;//enumerating classes that use forward declarationsvoidFoo (trafficlight*Light ) {    // ...}//... ..//Supplemental completion of the definition of an enumeration classenum classTrafficLight:Char{RED=1,//Red//... ..};

In this code, after we have completed the forward declaration of the enumeration class TrafficLight enum class, we can use it directly. And in the end, we just need to add a specific definition to it. In addition, we specify here that the underlying data type of the enumeration value is char, which is more resource-efficient than the default int.

As we can see from the above example, the use of enumeration classes is a good solution to the various problems encountered in the use of traditional enumeration types, so in future programming practices, we should prefer to use enumeration classes.

Hello, C + + (13) The answer to this single-choice question is a, B, C or d? 3.7 Enum Types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.