8th chapter: Structures, unions, and enumerations

Source: Internet
Author: User

This chapter mainly deals with some knowledge about structures,unions, and enumerations.

Structures:

1: We know that an array is a collection of elements of the same type, from the simplest defined point of view, we can assume that a struct is a collection of arbitrary types of elements, and that the default access level is public.

2: In memory, the order in which struct members assign addresses is assigned in the order in which they are declared, whereas the size of the struct object's memory is not simply the size of its member memory. In order to save allocated memory space, we usually put the type declaration that needs the maximum memory in the first place, need the minimum memory to put in the last, according to the memory size sort to declare. In general, however, we should arrange the declaration order of members based on readability, unless we really need to save space, so that we can arrange the order of member declarations from a large to a small sort of memory size.

3: As we mentioned above, it is understood from the simplest point of view that a struct can be considered as a collection of elements of any type. In fact, there is no difference between struct and class, except that the default member of struct is Public,class the default member is private. Like class, a struct can contain not only any type of element, but also member functions, as well as constructors.

4: Each struct has a unique definition, even if they have the same members. Such as:

struct s1 {int a;};struct s2 {int b;};s1 x;s2 y=x   // error: s1和s2是不同的类型;
Enumerations:

There are two types of enumerations, an enum defined by the old standard of C + +, and an enum class defined by the c++11 new standard. They have the following differences:

1: The scope of the enumeration member is different:

Enum enumeration members have the same scope (scope) as the enum, for example, if enum color { red,black,green}; defined in a global scope, then the enum member Red,black,green scope in the enum is global, and when we want to access its enumeration members, Can be written directly in red or black or green.

For enum class, however, the scope of its enumeration members is limited to the Enum class class, such as the assumption enum class color { red,black,green}; is defined in the global scope, but when we want to access its enumeration members, we have to write the form of color::red rather than just writing red.

So when we define multiple enum and enum classes in the same scope, the enum member names must not be the same for the enum, otherwise there will be compilation errors, and the Enum class can have the same enumeration member name. Like what:

enumTraffic_light { red,black,green};enumWarning

If the above two lines of code appear in the same scope, the compiler gives an error message stating that the enumeration member red and green are redefined.

enumclass Traffic_light { red,black,green};enumclass Warning { green,yellow,orange,red};

It is possible for these two lines of code to appear in the same scope because their enumeration members are confined to the traffic_light and warning scopes, and there are no name collisions.

Of course, the following code appears in the same scope as is possible:

enumclassTraffic_light {red,black,green};enumWarning{ green,yellow,orange,red};

2: For an enum, the value of its enumeration member can be implicitly converted to an integer value, but the Enum class is not available. However, an integer value cannot be implicitly converted to an enumeration member, either for Enum or enum class. But for this kind of mutual conversion without implicit definition, we can use static_cast () to display the definition. Examples are as follows:

//examples for enumenumColor {Red,black,green};color c0=0     //error, no int->color implicit conversion;intI1=red//okay,red is in the scope and converts int;Color c1=static_cast<color> (0)//okay. explicit conversion from int to color.//examples for enum class;enum classtraffic_light {Red,black,green}; Traffic_light tl1=0;//error, no int->traffic_light implicit conversion;inti2=traffic_light::red;//error, no traffic_light::red->int implicit conversion;Traffic_light tl2=static_cast<Traffic_light> (0)//okay. Explicit conversion from int to Traffic_lightinti3=static_cast<int> (traffic_light::red)//okay. Explicit conversion from traffic_light to int.

(3) When you define an enum, you can allow no names, such as

enum { arrow_up=1,arrow_down, arrow_sideways};

In general, we can do this when we only need a series of integer constants, but we do not need a type to define the variable. Of course, if we want to define the enum class, we must have a name, otherwise a compilation error will occur.

(4): Although the Enum class has some strict properties, we can think of the enum class as a class so that we can define the overloaded operator for the Enum class, which greatly expands the function of the enum class. In general, we should use the Enum class more than the enum, so as to avoid compilation errors as much as possible.

8th chapter: Structures, unions, and enumerations

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.