Union

My questions regarding union are
* Why union can not be used in inheritance?
* Why Union can not be a base class?
* Why a union can not have a virtual member function?
* Why no static variable can be member of a union?
Please help?
The union keyword comes from C which did not have classes and such, hence neither did unions in C++.

The technical difficulties surround unions stem from their very purpose: to be able to refer to the same
memory location with different names and different types. For example,

1
2
3
4
5
6
7
union U {
    int x;
    char c;
    double d;
};

U u;


The amount of memory allocated to the variable u is equal to the size of its largest member, which in this
case is d (6 bytes). But obviously if all three members occupy the same memory, changing one changes
them all, and depending upon which member I assign, the values of the others will be useless to me.
For example, if I set d to 3.1415, what would c be set to?

Having said all this, consider a class. All classes have at least one constructor, whether it be provided by
you or implicitly by the compiler (default constructor). The constructor _must_ run in order to set up the
vtables appropriately, and the vtable pointer is actually part of the class member data (albeit hidden), so
that the size of this class:

1
2
3
class Foo {
    virtual void Bar() {}
};


is in fact 4 bytes (on a 32 bit system).

So now, consider this hypothetical example:

1
2
3
4
5
6
7
union U {
    std::vector<int> v;
    std::string s;
    Foo f;
};

U u;


In order for you to use the v member, std::vector<int>'s default constructor had to be run to properly initialize the
vector to an empty container. In order for you to use the s member, std::string's default constructor had to be
run for the same reason. And in order for you to use f, Foo's default constructor had to be run.

But this is a problem for the compiler, because at the time of instantiation of U, it doesn't know which member you
are going to use, and it can't run all three constructors, because all three objects occupy the same memory, and
running the second constructor would blow away anything that the first constructor did, and so forth.

Hence the reason why C++ does not allow non-POD types and non-simple-aggregates as members of unions.
(Note: a simple aggregate is a class or struct containing no member functions, no destructor, no user-defined
constructors, and no non-POD types. POD type is any of the compiler intrinsic types -- int, char, double, bool,
pointer-to-anything)

Given that unions can't really support non-trivial members (my terminology), it doesn't make sense for unions
to be another synonym for class and struct.

Static variables occupy storage outside of the language construct that declared them. So for example:

1
2
3
4
5
6
struct S {
    int x;
    static int y;
};

S s;


Each instance of S contains an independent x, but there is only one y in the system, regardless of how many
instantiations of S there are. Static declarations are therefore orthogonal to the purpose of unions.
I should add one more thing:

C++0x is relaxing some of these restrictions. I encourage you to read
http://www2.research.att.com/~bs/C++0xFAQ.html#unions for more
details.
Topic archived. No new replies allowed.