union
s with Members of Class Type
Under earlier versions of C++, union
s could not have members of a class type that defined its own constructors or copy-control members. Under the new standard, this restriction is lifted. However, union
s with members that define their own constructors and/or copy-control members are more complicated to use than union
s that have members of built-in type.
When a union
has members of built-in type, we can use ordinary assignment to change the value that the union
holds. Not so for union
s that have members of nontrivial class types. When we switch the union
’s value to and from a member of class type, we must construct ...
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.