C ++ language guide (17th) -- other data types

Source: Internet
Author: User
**************************************** *****************
Original article: http://www.cplusplus.com/doc/tutorial/
**************************************** *****************

Other data types
Data Type defined (Typedef)C ++ allows us to define our own types based on the existing data types. We can use keywords TypedefIn the form: Typedef existing_type new_type_name;Where Existing_typeIs a basic type of C ++ or a composite type, New_type_nameIs the name of the new type we define. For example:


Your ad here

Typedef char C;Typedef unsigned int word;Typedef char * pchar;Typedef char field [50];
In this case, we define four data types: CharOf C, Unsigned IntOf Word, Char *Of PcharAnd act Char [50]Of FieldThen, we can use them in the declaration like any other valid type:
C mychar, anotherchar, * ptc1;Word myword;Pchar ptc2;Field name;
TypedefDifferent types are not created. It only creates synonyms of existing types. That means MywordCan be considered WordIt can also be considered Unsigned intBecause the two are actually of the same type. TypedefIt is very useful for defining aliases of types that are frequently used in a program. It is also useful for defining those types that we may need to change in the future to change our program, or it is useful if a type you want to use has a long or confusing name. Shared body ( Unions )The shared body allows the same memory to be accessed as different data types, because they are all in the same memory area. Its declaration is similar to the use of the same structure, but its functions are completely different: Union union_name {
Member_type1 member_name1;
Member_type2 member_name2;
Member_type3 member_name3;
.
.
} Object_names;
All elements declared in the shared body occupy the same physical space in the memory. Its size is the size of the largest element in the declared element. For example:
Union mytypes_t {Char C;Int I;Float F;} Mytypes;
Define three elements:
Mytypes. cMytypes. IMytypes. f
Each one has a different data type. Because they all use the same region in the memory, modifications to one of the elements will affect the values of all elements. We cannot store different values for each of them separately from others. A common object may be a combination of a basic type and an array or a struct with smaller elements. For example:
Union mix_t {Long L;Struct {Short Hi;Short lo;} S;Char C [4];} Mix;
Three names that can access the same group of 4 bytes are defined: Mix. L, Mix. sAnd Mix. cWe can use these names based on how we want to access these bytes. We must consider them as a separate LongType data, as two ShortElement or CharArray of elements. I have mixed the types. In this shared body, there are arrays and struct, so you can understand the different methods we can access the data. For Little-Endian(Small EndianIn a computer memory system (mainly a PC platform) that stores the least important bytes with the smallest address (and the most important bytes with the largest address), this shared body can be depicted:
The exact combination and the sequence of the shared body members in the memory are platform-related. Therefore, pay attention to the possible problems caused by using this type. Anonymous ( Anonymous ) Shared bodyIn C ++, we can choose to declare an anonymous shared body. If we declare a shared body without any names, the shared body will be anonymous, and we can directly access their members through their member names. For example, pay attention to the differences between the following two struct declarations:
Struct with common shared body Struct with an anonymous shared body
Struct {
Char title [50];
Char author [50];
Union {
Float dollars;
Int yens;
} Price;
} Book;
Struct {
Char title [50];
Char author [50];
Union {
Float dollars;
Int yens;
};
} Book;
The only difference between the two code snippets is that in the first one, we give the shared body a name ( Price), But we didn't give it in the second place. When we access a member of this type of object DollarsAnd YensYou can see the difference. For an object of the first type, it should be:
Book. Price. dollarsBook. Price. yens
For an object of the second type, it should be:
Book. dollarsBook. yens
I remind you again: because it is a shared object instead of a struct, members of dollars and yens occupy the same physical space in the memory, so they cannot store two different values at the same time. You can set a value for price in dollars or yens if they are not the same. Enumeration (Enum)Enumeration creates new data types that contain different values. It contains values not limited to those that can be included in basic data types. The format is as follows: Enum enumeration_name {
Value1,
Value2,
Value3,
.
.
} Object_names;
For example, we can use the following statement to create ColorThe new variable type used to store colors:
Enum colors_t {black, blue, green, cyan, red, purple, yellow, white };
Note that the Declaration does not contain any basic data types. From a certain point of view, we have created a brand new data type, which is not based on any other existing types from the very beginning. This new type Color_tThe variable may contain values that are new constants enclosed in curly braces. For example Colors_tDeclared, the following expression will be valid:
Colors_t mycolor; Mycolor = blue;If (mycolor = green) mycolor = red;
Enumeration is compatible with numeric variables, so their constants are usually assigned an integer value internally. Unless otherwise specified, the integer value corresponding to a possible value is equal to 0, and the subsequent A + 1Rules. Therefore, the data type defined above Colors_tMedium, BlackEquivalent to 0, BlueWill be equivalent to 1, GreenIt is equivalent to 2, and so on. We can specify an integer value for any constant that can be included in our Enumeration type. If the constant following it (a constant with a specified value) is not given an integer value, it will be automatically assigned the value of the previous constant plus one. For example:
Enum months_t {January = 1, February, March, limit l,May, June, July, August,September, October, November, December} Y2K;
In this case, the months_t type variable Y2K contains any 12 possible values, from January (January) to December (December) the equivalent value ranges from 1 to 12 (instead of from 0 to 11, because January (January) is equivalent to 1 ).

 

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.