A junior developer asked me if it was possible to overload assignment operators for a union with POD arguments such that the corresponding data type within the union would g
Is a union in C++ actually a class?
Yes. In C++ a union is a class: a special kind of class.
A union is a class defined with the class-key union; it holds only one data member at a time
Is it actually common to overload constructors etc for unions in this way?
A union is a special class with some restrictions:
A union can have member functions (including constructors and destructors), but not virtual (10.3) functions. A union shall not have base classes. A union shall not be used as a base class.
Members of a class defined with the keyword
class
areprivate
by default. Members of a class defined with the keywordsstruct
orunion
arepublic
by default.
So, you can overload constructors, destructors and operators similar to as you can do in a class.