Detailed description of Enum

Source: Internet
Author: User

Definition: Creates a symbolic constant.
From its definition, we can see that Enum is used to define constants. Unlike const, a const is a set of constants that contain multiple constant values.

For example:

  enum color{red,green,yellow,blue};

That is to say, for the constant color, the values of red, green, yellow, and blue can be taken. They correspond to integer 0, 1, 2, and 3. These constants are called enumeration. By default, an integer is assigned to the enumeration quantity. The value of the first enumeration is 0, the value of the second enumeration is 1, and so on.

Of course, you can also customize the integer corresponding to them. The method is as follows:

   enum color{red,green = 10,yellow = 20,blue};

In this definition, the integer corresponding to Red is still 0, while green and yellow have assigned a value to it, so the original default value is overwritten, the value is the value corresponding to yellow.

Add 1 to the integer, that is, 21.

Defects: The constants defined in enumeration are the same as those defined in enumeration, rather than the enumeration itself.

For example:

Color Col = red; // correct Col =: green; // correct Col = color: Blue; // Error

It is precisely because of this defect that constant values in enumeration cannot be repeatedly defined in the same file.

For example:

 enum test1{first,second = 10};enum test2{first,second,third};

When the program is compiled, the following error occurs: conflicting declaration 'second '.

Note:: Because the enumerated quantity is an integer type, it can be upgraded to the int type, but the int type cannot be automatically converted to the enumeration type.

For example:

        int num = green;int m = red + 10;

Both methods are valid!
Storage space occupied:When we see this definition method, we can't help but try to test its sizeof () value. Yes, the result is a little unexpected; the output is 4 (GCC compiler ). Why? This is related to the specific compiler: for enumeration with a smaller value range, it is sufficient to use one byte or less space. However, for enumerations containing the long type, 4 bytes are used.

#include <iostream>#include <string>using std::cin;using std::cout;using std::endl;using std::string;enum color{        red,        green,        yellow,        blue};int main(void){    int id;    cout << "Please input the number of color:";    cin >> id;    switch (id)    {        case red:            cout << "You choose red!"<< endl;            break;        case green:            cout << "You choose green!"<< endl;            break;        case yellow:            cout << "You choose yellow!"<< endl;            break;        case  blue:            cout << "You choose blue!"<< endl;            break;        default:            ;    }    return 0;}

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: 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.