Unions

A union is a class type in which all data members share the same memory location. In C++, a union may include both member functions and data. In
a union, all of its members are public by default. To create private elements, you must use the private keyword. The general form for declaration of
a union is

union class-name {
 // public members by default
private:
 // private members
} object-list;

In C, unions may contain only data members and the private keyword is not supported.

The elements of a union overlay each other. For example,

union tom {   char ch;   int x; } t;

declares union tom, which looks like this in memory (assuming 2-byte integers):

click to expand

Like a class, the individual variables that comprise the union are referenced using the dot operator. The arrow operator is used with a pointer to a union.

There are several restrictions that apply to unions. First, a union cannot inherit any other class of any type. A union cannot be a base class. A union cannot have virtual member functions. No members may be declared as static. A reference member cannot be used. A union cannot have as a member any object that overloads the = operator. Finally, no object can be a member of a union if the object’s class explicitly defines a constructor or destructor function. (Objects that have only the default constructors and destructors are acceptable.)

Programming Tip 

In C++, it is common practice to use struct when creating C-style structures that include only data members. A class is usually reserved for creating classes that contain function members. Sometimes the acronym POD is used to describe a C-style structure. POD stands for Plain Old Data.

There is a special type of union in C++ called an anonymous union. An anonymous union declaration does not contain a class name and no objects of that union are declared. Instead, an anonymous union simply tells the compiler that its member variables are to share the same memory location. However, the variables themselves are referred to directly, without using the normal dot or arrow operator syntax. The variables that make up an anonymous union are at the same scope level as any other variable declared within the same block. This implies that the union variable names must not conflict with any other names valid within their scope. For example, here is an anonymous union:

union { // anonymous union   int a;   // a and f share   float f; // the same memory location }; // ... a = 10; // access a cout << f; // access f

Here, a and f both share the same memory location. As you can see, the names of the union variables are referred to directly without the use of the dot or arrow operator.

All restrictions that apply to unions in general apply to anonymous unions. In addition, anonymous unions must contain only data—no member functions are allowed. Anonymous unions may not contain the private or protected keywords. Finally, an anonymous union with namespace scope must be declared as static.




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net