Skip to content

Last updated:2026-10-12

C++ union

About the Author:

Personal WeChat official account:

wechat

A union is a space-saving special kind of class: a union can have multiple data members, but at any moment only one of them can have a value. When some member is assigned, the other members become undefined. Unions have the following characteristics:

  • The default access specifier is public
  • They may have constructors and destructors
  • They may not have members of reference type
  • They may not inherit from other classes and may not serve as base classes
  • They may not have virtual functions
  • The members of an anonymous union can be accessed directly in the scope where the union is defined
  • An anonymous union may not contain protected or private members
  • A global anonymous union must be static

Comments