C++ essentials 之 union
Extraction from The C++ Programming Language 4th. edition, Bjarne Stroustrup
- [8.1] A
struct
is a sequence of elements (called members) of arbitrary types. Aunion
is astruct
that holds the value of just one of its elements at any one time. - A
union
is astruct
in which all members are allocated at the same address so that theunion
occupies only as much space as its largest member. Naturally, aunion
can hold a value for only one member at a time.