Table of Contents
Previous Section Next Section

union

A union is a special type of class that assigns two or more variables to the same memory location. The form of its definition and the way the . (dot)
and –> (arrow) operators reference a member are the same as for a class. By default, a union's members are public. The general form is

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

The class-name is the type name for the union.

NOTE: In C, unions can contain only data members and the private specifier is not allowed.

For example, this creates a union between a double and a character string and creates one variable called my_var.

union my_union {
  char time[30];
  double offset;
} my_var;

The union is covered in more detail in Chapter 1.


Table of Contents
Previous Section Next Section