Discussion on Enum in C ++

Source: Internet
Author: User

If a variable requires several possible values, it can be defined as an enumeration type. The reason for enumeration is to enumerate the possible values of variables or objects.

For example, to make everyone better understand, for example, there is a pen in a pencil box, but you don't know what it is before you open it, it may be a pencil or a pen. There are two possibilities, so you can define an enumeration type to represent it!

EnumBox {penpencil, pen };// Here you define a variable of the enumeration type, box. This enumerated variable contains two elements, also known as the enumeration element, which are the pencil and pen, respectively.

If you want to define two variables with the same Enumeration type, you can use the following two methods to define them!

EnumBox {penpencil, pen };

EnumBox box2;// Or abbreviated as box box2;

Another way is to define the statement at the same time.

Enum{Penpencil, pen} box, box2;// Define the statement at the same time!

The system of enumeration elements in enumeration variables isProcess by constantIs calledEnumerated constantThey cannot assign values to ordinary arithmetic tasks. Writing and sending such a statement is incorrect, but you can assign values when declaring it!

EnumBox {penpencil = 1, pen = 2 };

However, if you do not assign values to elements, the system automatically increments the values from 0, if you only define the first element, the system adds 1 to the value of the previous element to the next element. For example

EnumBox {penpencil = 3, pen };// Here, pen is 4. The system will automatically assign values to pen = 4!

As mentioned above, the following is a complete example.CodeMore complete learning!

# Include <Iostream>
Using NamespaceSTD;

Void Main(Void)
{
EnumEgg {a, B, c };
EnumEgg test;// Here you can abbreviated it as egg test;

Test=C;// Grant the element operation to the enumerated variable test. Here, the element assignment operation is not called the value assignment operation to make it clear that the enumerated variable cannot be directly assigned a value, such operations as (test = 1;) are not accepted by the compiler. The correct method is to force type conversion first. For example=(Enum egg) 0 ;)!

If(Test = C)
{
Cout<"Enumeration variable judgment: The enumeration element corresponding to the test enumeration is C" <Endl;
}

If(Test = 2)
{
Cout<"Enumeration variable judgment: the value of the test enumeration element is 2" <Endl;
}

Cout<A <"|" <B <"|" <test <Endl;

Test=(EnumEgg) 0;// Force type conversion
Cout<"The test value of the enumerated variable is changed to:" <test <Endl;
CIN. Get ();
}

The final problem here is that the enumerated elements (or enumeration constants) in the enumerated variables will beAutomatically upgraded to arithmetic typeOf!

# Include <Iostream>
Using NamespaceSTD;

Void Main(Void)
{
EnumTest {a, B };
IntC = 1 + B;// Automatically upgraded to arithmetic type
Cout<C <Endl;
CIN. Get ();
}

Enum is a user-defined type. It has data members and member functions!
For example:
Enum E {A = 1, B = 2, c = 4 };
So:
001: Enum e E1; // Enum e is not an object, it is a type, and E1 is an object of the type Enum!
002: E E1; // E is short for Type Enum e!

003: e1 = 1 ;//Absolute Error!How can int be assigned to a user-defined type?
004: e1 = E (); // E ()? Yes, you are not mistaken. default constructor
005: e1 = E (1) // E (INT )? Hey, this is the constructor that constructs the enum E type object from Int.
006: e1 = A; // Haha, the "copy constructor" is called by default.

Think boldly: Will Enum have user-defined member functions? I did not try it out. Do you want to try it yourself?
Feeling: Although C ++ is an early OO language, the concept of typing has penetrated into the language itself. In the face of an OO language, you must know one thing:Everything is an object and everything has a type ···

"Value range" and "memory allocation" of Enum"

First, correct a common error. Many people think that Enum is a discrete set, which is too idealistic ^ _ ^. Simply think about it and you will be able to solve it. If you don't talk about it, let's get started:
How to determine the value range of an Enum?
For example:
Enum E1 {A = 2, B = 4 };

First, find the maximum value of its absolute value, but to make it easy to understand, I will not talk about negative numbers, that is, find its maximum value first. Here the maximum value is 4.
4 is represented as 100 in binary format, that is, 3bits is required to have the minimum value of 4. The value range of 3bits is 0-7, so the value range of E1 is [].

Now let's look at the negative number,
Enum E2 {A =-2, B = 4 };
The maximum absolute value is 4, which requires 3 bits to accommodate, but because it can take a negative value (while the maximum element B = 4 is not a negative value), that is to say, you need to add a symbol bit, 4 bits is required.
The value range of 4bits is 1000-0111 (in binary format), that is,-8 to 7 (in decimal format ).
Enum E3 {A =-4, B = 2} Only needs 3 bits, the value range is [-4, 3].

Simply put, we can find the minimum number of digits that can hold all elements.
Why is the enum value range required?BecauseThe C ++ Standard specifies that the value assignment result beyond the enumerated type is undefined..
That is to say, E2 x = (E2) 6 is certainly correct, while E2 y = (E2) 8 is undefined.
I will not talk much about the meaning of undefined. If you want to solve it, you can do it. ^_^

What about Enum memory allocation?

For example, if E2 requires 3 bits, C ++ specifies the E2 size.As long as it can accommodate3 bitsJust doWhether to take 1 byte, 4 byte, or..., thenDetermined by the compiler. However, the C ++ standard has a limit: 1 <= sizeof (enmu) <= sizeof (INT ). Hey.

Original article: http://hi.baidu.com/wy_51131/blog/item/7251e7cf45d38d0592457e87.html#0

Related Article

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.