Union (Union) is not much in C / C , but in some places for memory requirements, the United States has frequently appeared, then what is the union, how to use, what do you need to pay attention to? In these questions, I tried to do some simple answers, there is definitely in the place, welcome to point out!
1, what is the joint? "Union" is a special class and a data structure of a structure type. In a "combination", a variety of different data types can be defined, one of the variables described as the "federated" type, allowing any of the data defined by the "United", which share the same period of memory, The purpose of saving space has been achieved (there is also a space-saving type: bit field). This is a very special place and is also a combined feature. In addition, like Struct, joint default access rights are also public, and also have member functions.
2, the difference between the joint and structure? "Union" has some similarities between "structure". But both have different inherent. In the structure, the members have their own memory space, and the total length of a structural variable is the sum of each member (except for the empty structure, and the boundary adjustment is not considered). In the "United", each member share a memory space, and a combined variable is equal to the longest length in each member. It should be noted that this so-called sharing does not mean that multiple members are simultaneously loaded into a combined variable, but means that the combined variable can be given to any member value, but only one value can be assigned each time. The value is rushed away.
3, how to define? For example: union test {test () {} int office; char teacher [5];}; defined a joint type named TEST, it contains two members, one is intellectual, member name is Office; another is The character array is called Teacher. After the joint definition, a combined variable description can be performed as a variable of the Test type, which can be stored in an integer OFFICE or a character array Teacher.
4, how to explain? Three forms of joint variables: first definition, definition, definition, and direct description. Take the Test type as an example, described below: 1) Union test {Int Office; char TEACHER [5];}; union test a, b; / * Description A, B is Test type * / 2) union test {Int Office; Char Teacher [5];} A, B; 3) Union {Int Office; char TEACHER [5];} a, b; the N-B variables are TEST type. A, the length of the B variable should be equal to the longest length in the members of TEST, ie, equal to the length of the Teacher array, a total of 5 bytes. A, B variables When it gives an integer value, only 4 bytes are used, and 5 bytes can be used when applying the character array.
5, how to use it? The assignment of the combined variable can only be carried out by a member of the variable. Members of the combined variable are represented as: a combined variable name. For example, A. A. Class, A.Office does not allow only combined variable names to assign or other operations, not allowed to use the combined variable name. The combined variable is assigned to assume the value, and the assignment can only be done in the program. Also emphasized that a combined variable can only give a member value each time. In other words, the value of a combined variable is a member value of a joint committee. 6. Anonymous joint anonymous combination only notify the compiler that its member variables share an address, and the variable itself is directly referenced, not using the usual point operator syntax. For example: #include
7, some places that need to be discussed: 1, what do those things can be stored in the United States? We know that there is a sharing of memory in a joint, so static, reference is not used because they cannot share memory. 2, can the class can be put in a joint? Let's take an example: Class test {public: test (): data (0) {} private: int data;