How to use Union in c++?

In c++, we should keep using c style union. Although c++ union supports constructor, we'd better not use it. Actually, no need to use constructor for union basically. Going deeper, even as the expanded union in c++, it can't support constructor, destructor, copy-constructor, operator=, and virtual function class level operations on its members. Like you can't put CString, Struct variables as a c++ union's members.

The only two differences between a C++ struct and a class are that, by default, the members of a class are private, while the members of a struct are public and that structs are inherited from publically by default while classes are inherited from privately by default. Aside from these distinctions, structs and classes perform exactly the same function.

你可能感兴趣的:(UNION)