Chapter 11 special tools and technologies (11)

Source: Internet
Author: User

Previous: http://www.bkjia.com/kf/201201/116372.html

18.5 Union: space-saving class
Union is a special class. A union object can have multiple data members, but only one member can have a value at any time. When a value is assigned to a member of the union object, all other members become undefined.
The amount of storage allocated to a union object must be at least the same as that of its largest data member. Like any class, a union defines a new type.
1. Define Union
Union provides a convenient way to represent a group of mutually exclusive values, which can be of different types.
A union defines union names starting with the keyword union, followed by (optional) union names, and a group of member declarations enclosed in curly brackets.
Class Andersen sclass {};

Union UnionClass {
Char * cval;
Int ival;
Double dval;
Andresclass anders;
};

Static union {
Char cval;
Int * ival;
Double dval;
Andresclass anders;
};
2. No static data member, referenced member, or class data member
Some (but not all) class features also apply to union. For example, like any class, union can specify protection tags to make members public, private, or protected. By default, union performs like struct: Unless otherwise specified, the union members are all public members.
Union can also define member functions, including constructor and destructor. However, union cannot be used as a base class, so the member function cannot be a virtual number.
Union cannot have static data members or reference members, and union cannot have class members that define constructors, destructor, or assignment operators.
3. Use the Union type
The union name is a type name. Like other built-in types, union objects are not initialized by default. You can use the same method as explicitly initializing a simple class object to explicitly initialize the union object. However, the initialization type can only be provided for the first member. The initialization must be included in a pair of curly braces.
UnionClass uc = {new char ('A ')};
4. Members using the Union
You can use the regular member access operator (. And->) to access members of a union object.
UnionClass uc = {new char ('A ')};
Cout <* uc. cval <endl;
Assign a value to a data member of the union object so that other data members become undefined. When using the union object, we must always know what type of value is currently stored in the union object. Retrieving the value stored in the union object through an incorrect data member may cause program crash or other incorrect program behavior.
The best way to avoid access to union values by incorrect members is to define what values are stored in a separate object trace union. This additional object is called the union discriminant (discriminant ).
5. nested Union
Union is most often used as a nested type, where the discriminant is a member of the peripheral class.
Class TheClass {
Public:
Enum ClassKind {INT, CHAR, DBL };
ClassKind ck;
Union {
Char cval;
Int ival;
Double dval;
} Val;
};
TheClass tc = TheClass ();
Tc. val. cval = '1 ';
6. Anonymous Union
An unnamed union that is not used to define an object is called an anonymous union ). The name of an anonymous union member appears in the peripheral scope.
Class TheClass2
{
Public:
Union {
Char cval;
Int ival;
Double dval;
};
};
TheClass2 tc2 = TheClass2 ();
Tc2.cval = '1 ';
Anonymous union cannot have private or protected members, nor define member functions.

From xufei96's column
 

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.