C/c++:[2]enum-enumeration declaration, definition, and use

Source: Internet
Author: User

C/c++:[2]enum-enumeration declaration, definition, and use

Transferred from: http://jingyan.baidu.com/article/e75aca85526c1b142edac6d9.html

As is known to all, C + + languages can use #define and const to create symbolic constants, and the enum tool can not only create symbolic constants, but also define new data types, but must follow certain rules, Let's look at how the enum is used.

Tools/Materials
    • Microsoft Visual Studio 2012 (or other version)

Declaration and definition of an enumeration of steps
  1. 1

    First, take a look at the following statement:

    Enum Enumtype {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

    This sentence has two functions:

    First: Declare enumtype as a new data type, called an enumeration (enumeration);

    Second: Declaring Monday, Tuesday, etc. as symbolic constants, often referred to as enumerators , whose values default to 0-6. (We'll show you how to explicitly initialize the value of an enumerator later)

  2. 2

    The new enumeration type Enumtype is then used to declare variables of this type:

    Enumtype Weekday;

    Just like using the basic variable type int to declare a variable, such as int A;

    However, unlike the basic variable type, you can only assign a defined enumerator to a variable of that enumeration without casting , such as:

    Weekday = Monday;

    Or

    Weekday = Sunday;

    You cannot assign other values to an enumeration variable , such as:

    Weekday = 10;

    This is not allowed because 10 is not an enumerator .

    That is to say, weekday can only be defined monday-sunday these defined enumerated amounts .

    This is not absolute, however, and sixth is about assigning other type values to enumeration variables using coercion type conversions.

  3. 3

    If you cannot assign a non- enumerator to an enumeration variable , can you assign an enumerator to a non-enumeration variable ? Such as:

    int a=monday;

    This is allowed because the enumeration is a symbolic constant, and the assignment compiler here automatically converts the enumerator to the int type.

  4. 4

    It is possible to perform an assignment operation on an enumeration, can the enumeration variable perform arithmetic operations?

    weekday++;

    Weekday = Monday + Tuesday;

    This is illegal because these operations can result in a violation of type restrictions, such as:

    Weekday = Sunday;

    weekday++;

    Weekday is first given the last value in the enumerator Sunday (a value of 6), and then incremented, weekday increases to 7, and 7 is not valid for the Enumtype type.

    Summary: For enumerations, only assignment operators are defined, and arithmetic operations are not defined for enumerations.

  5. 5

    You cannot perform arithmetic operations on an enumerator , so can an enumerator participate in the operation of other types of variables?

    int A;

    A = 1 + Monday;

    This is allowed because the compiler automatically converts the enumerator to the int type.

  6. 6

    Second: Without coercion, you can only assign a defined enumeration to a variable of that enumeration, meaning that you can assign other type values to the enumeration variable by casting:

    Weekday = Enumtype (2);

    Equivalent to:

    Weekday = Wednesday;

    However , what happens if you attempt to assign a value beyond the scope of an enumeration to an enumeration variable by casting it?

    Weekday = Enumtype (20);

    The result will be indeterminate, and doing so will not go wrong, but you won't get the results you want.

    END
Step 2--The value of the custom enumeration amount
    1. 1

      The previous talk by defining

      Enum Enumtype {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

      Enumeration Amount The values for Monday, Tuesday, and so on are 0-6 by default, and we can explicitly set the value of the enumerator :

      Enum Enumtype {monday=1, tuesday=2, wednesday=3, thursday=4, friday=5, saturday=6, sunday=7};

      The specified value must be an integer !

    2. 2

      You can also explicitly define only a subset of the value of an enumerator :

      Enum Enumtype {monday=1, Tuesday, Wednesday=1, Thursday, Friday, Saturday, Sunday};

      So that Monday and Wednesday are defined as 1, the values for Tuesday=2,thursday, Friday, Saturday, and Sunday default to 2, 3, 4, and 5, respectively.

      Summary: The value of an uninitialized enumeration value defaults to 1 larger than the enumerated value preceding it.

    3. 3

      The second article also shows that the value of the enumerator can be the same.

      END
Step 3--The value range of the enumeration
    1. 1

      As mentioned earlier, you can assign other type values to an enumeration variable by casting:

      Weekday = Enumtype (2);

      This is legal;

      Weekday = Enumtype (20); it's illegal.

      This involves the concept of enumerating the range of values :

      The upper bound of the enumeration is the power of the smallest 2 greater than the maximum enumerator , minus 1;

      There are two conditions for the lower bound of the enumeration: first, the minimum value of the enumeration is not less than 0, the enumeration lower bound is 0, and the minimum value of the enumeration is less than 0, then the lower bound of the enumeration is the power of the largest 2 less than the minimum enumerator , plus 1.

      For example:

      If you define enum EnumType1 {first=-5,second=14,third=10};

      The upper bound of the enumeration is 16-1=15 (16 is greater than the maximum enumerator 14 and is a power of 2);

      The lower bound of the enumeration is -8+1=-7 (8 is less than the minimum enumerator -5, and is a power of 2);

      END
Step 4--Enumerate the application
    1. Individuals feel that enumerations and switch are the best partners:

      Enum Enumtype{step0, Step1, step2}step=step0;

      / /Note that the enumeration variable step is defined directly at the time the enumeration is declared and initialized to Step0

      Switch (Step)

      {

      Case step0:{...; break;}

      Case step1:{...; break;}

      Case step2:{...; break;}

      Default:break;

      }

      END
Precautions
  • In practical applications, enumerations are often used to define symbolic constants.

    The example code that you implement yourself is as follows:

    1#include <iostream>2 3 4 enumWeektype5 {6Sun =0,7Mon =1,8 Tue,9 };Ten  One voidCallweek (weektype wk) A { -     Switch(wk) -     { the      CaseSun: -std::cout<<"Monday ..."<<Std::endl; -          Break; -      CaseMon: +std::cout<<"Tuesday ..."<<Std::endl; -          Break; +      CaseTue: Astd::cout<<"Wednesday ..."<<Std::endl; at          Break; -     default: -std::cout<<"Waring ..."<<Std::endl; -          Break; -     } -  in } -  to  +  - intMainintargcChar**argv) the { *Weektype iwk=Weektype::mon; $     intA =Weektype::tue;Panax Notoginseng  -     Switch(a) the     { +          Case 0: Astd::cout<<"Monday ..."<<Std::endl; the              Break; +          Case 1: -std::cout<<"Tuesday ..."<<Std::endl; $              Break; $          Case 2: -std::cout<<"Wednesday ..."<<Std::endl; -              Break; the         default: -std::cout<<"Waring ..."<<Std::endl;Wuyi              Break; the     } -  Wu Callweek (iWk); -  About     return 0; $}
    View Code

C/c++:[2]enum-enumeration declaration, definition, and 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.