Unions allow more than one object, of either class or builtin type, to occupy the same space in memory. For example:
// Example 1
//
union U
{
int i;
float f;
};
U u;
u.i = 42; // ok, now i is active
std::cout << u.i << std::endl;
u.f = 3.14f; // ok, now f is active
std::cout <<>
But only one of the types can be "active" at a time -- after all, the storage can after all only hold one value at a time. Also, unions only support some kinds of types, which leads us into the next question:
No comments:
Post a Comment