The role of C + + enumeration types __c++

Source: Internet
Author: User

What is the enumeration type and what is the use of it?

Enumeration types are user-defined, because they represent a meaningful set of element values.

For example, the colors that are known to be in the system are blue, red, black, and green. So you can define an enumeration type to represent these colors, where each enumeration member represents blue, red, black, and green.

Ii. How to define an enumeration type

In C + +, the keyword enum is used to define the

Enum enumeration type name {A series of enumerated values};

Enum Colors{blue,black,red,green};

Where an enum is a keyword that defines an enumeration type. Colors are enumerated type names, and blue is enumerated constants.

We then define an enumeration type and specify the desired value for the variable of that enumeration type.

Value of the enumeration member

Enumeration members are values that are desirable for enumeration types when defining enumerated types.

By default, the first enumerated constant value in an enumeration type is 0, followed by each enumerated constant in +1.

We can also specify enumeration member values, which are done when the enumeration type is defined, and cannot be modified after the enumeration type is defined.

For example

Enum Colors{blue=5,black=3,red,green};
So we modify the enumeration member values, and then the Red,green are added by the value of blue.

PS. Only constant expressions can be used when modifying an enumeration member (values are determined at compile time).

In this case, the blue and green values are all 5, which is not a compile error, but this is a bad behavior.

Because we use enumerated types to have a different set of values to represent different states, if an object colors col3 value equals blue, then he is also green. Therefore, it is not possible to distinguish between the meanings represented by this enumeration variable when used.

Iii. How to define the use of enumeration variables

We have defined an enum type colors, but we can't use this enum type directly when we use it, we need to use an enumeration variable or an enumeration member (to define what is in the enum type bracket).

Defining an enumeration variable is similar to a normal type

Colors col1;

You can also define enumeration variables at the same time that you define an enumeration type, as you would for a generic type object.

Enumeration variables can only be assigned with enumeration members and variables of the same type, and integer constants cannot be used.

Because we cannot specify the conversion rules for an integer constant to an enumeration type, the compiler does not help us define it, so it cannot be done.

Of course we can use the cast operator to implement conversions between types.
Iv. commonly used operators for enumeration variables

Commonly used operators have output operator <<, each comparison operator, assignment operator, etc.

The output operator outputs an integer value represented by an enumeration variable.

Comparison operators can only compare enumeration variables and enumeration member values of the same type.

The right-hand operand of an assignment operator is also intelligent as an enumeration variable of the same type and an enumeration member value.

V. Scope of Enumeration members

As long as the definition of the enumeration type is visible, you can use the enumeration member values directly.

For example, we can define an enumeration type in a class, so we can use enumeration members and enumeration variables directly in the class.

Note: Enumeration types, enumeration members, enumeration variables, and so on, are generally not difficult to use.

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.