Last updated:2026-10-12
C++ union¶
About the Author:¶
Personal WeChat official account:

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
Related Topics¶
- C++ Pure Virtual Functions and Abstract Classes | C++那些事
- C++ assert | C++那些事
- C++ Bit Fields | C++那些事
- Implementing C++ Polymorphism in C | C++那些事
- C++ const Constants and Qualifiers | C++那些事
- The Complete C++ const Family | C++那些事
- C++ Dereference Operator * | C++那些事
- C++ Initialization and Assignment | C++那些事
- Ways to Return Multiple Values in C++ | C++那些事
- C++ mutable Keyword | C++那些事