RAD Studio
ContentsIndex
PreviousUpNext
Union Declarations

The general declaration syntax for unions is similar to that for structures. The differences are:

  • Unions can contain bit fields, but only one can be active. They all start at the beginning of the union. (Note that, because bit fields are machine dependent, they can pose problems when writing portable code.)
  • Unlike C++ structures, C++ union types cannot use the class access specifiers: public, private, and protected. All fields of a union are public.
  • Unions can be initialized only through their first declared member:

union local87 {
  int i;
  double d;
  } a = { 20 };

A union can't participate in a class hierarchy. It can't be derived from any class, nor can it be a base class. A union can have a constructor.

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!