How enum types are used enum

Source: Internet
Author: User

the method of using enumeration types is generally defined as follows:enumenum_type_name{enum_const_1, enum_const_2, ... Enum_const_n} enum_variable_name; Note: Enum_type_name is a custom data data type name, and Enum_variable_name is a variable of type enum_type_name, That's what we usually call enumeration variables. In fact, the Enum_type_name type is the qualification of the value range of a variable, and the curly brace is its range of values, that is, the variable enum_variable_name of the Enum_type_name type can only be evaluated as any one of the values in curly braces. If the value assigned to the type variable is not in the list, an error or warning is given. Enum_const_1, enum_const_2 、...、 Enum_const_n, these members are constants, which is what we normally call enumeration constants (constants are generally capitalized). enumvariable types can also assign values to the constant symbols in them, and if they are not assigned, they are incremented from the constant that is assigned the initial value by 1, and if none are assigned, their values increment by 1, starting with 0. If you use a constant to indicate a different color:enumcolor{GREEN=1, RED, BLUE, green_red=Ten, Green_blue} Colorval, where each constant name represents a value of: GREEN=1RED=2BLUE=3green_red=TenGreen_blue= Onesecond, enumeration and#defineThe difference between macrosHere's another look at enumerations and#defineThe difference between macros:1)#defineMacro constants are simple replacements during the precompilation phase. Enumeration constants determine their values at compile time.2typically in the compiler, you can debug enumeration constants, but you cannot debug macro constants. 3Enumeration can define a large number of related constants at a time, and#defineA macro can only define one at a time.leave two questions: a), enumerate what can be done,#defineCan a macro do all that? If so, why do I need to enumerate?B),sizeofwhat is the value of (colorval)? Why? The enum definition is similar to the followingenumColor {Red, Gray, Blue}; it might look like this: color color=Red;Switch(color) { CaseRed: ... CaseGray: ... CaseBlue: ...}2first say why Red, can not write color::red, the latter kind of writing seems to be more in line with our habits. The reason is: in C++, the constants defined in the enumeration type belong to the scope that defines the enumeration, not to the enumeration type; We can write this: color color=: : Red; Can't write like this: color color=color::red3with regard to the size of memory used by enumeration types, there is a post in StackOverflow that the individual thinks is more reasonable to interpret. One of the explanations is that the reason is 4 bytes because the enum type is stored as an int. 

How enum types are used enum

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.