Unions in Cplusplus.
So unions, as a cousin of the pod that puts all of its member in the same place.
So you can think of unions as a different views or interpretations of a block of a memory so they can
be useful in some low level situations, such as when marshalling structures that must be consistent
across architectures.
So dealing with the type checking issues related to C, Cplusplus, Interoperation and even packing
Bitfields So let's create a union here, a union here, union variant here variant.
As you can see here, this is this definition is same as struct.
So in previous lecture we defined.
Struct and create an example from it.
And so let's create a character.
My string here ten then integer my integer here.
A double.
Double my.
My double, for example.
So then the union here variant can be interpreted as my string ten an integer or a double.
So it takes up only as much memory as it largest member.
Probably a string here.
My string in this case.
So you can use that operator to specify a union's interpretation.
Syntactically this looks like this looks like accessing a member of Pod, but it's completely different
under the hood here.
So let's create an variant here.
Variant V.
Here we that integer, my integer, for example, 32 and printf.
The answer here answer.
Here.
We that integer.
My integer.
And let's define the that floating point here.
We got my double.
My double.
Yes.
And here two point or three point here this is the double and then print f.
My double.
Or let's make it my integer.
My union integer.
And my union double.
Here or here?
Here.
My union double D and.
We that.
My double.
And then here.
Print test.
My union integer.
Uh, print here.
My union integer two, for example.
Here d v.my integer.
Actually, we have we used the wrong format specifier here we have to use F okay, so.
Let's run our program now.
So let's run here.
Oops.
Actually, let's add the new lines here.
Okay.
New line.
Oops.
New line and new line.
Here.
As you can see, this is the weird answer we got here.
So firstly we declared variant V.
Next we interpret we as an integer so and set its value to 32 here.
And then print it.
So you hear then reinterpret the.
As a float and reassign its value here.
So you printed the the to the console and it appears well as you as we've righted it so far so good
here.
So disaster strikes only when you try to interrupt V as an integer again here.
As you can see, our integer is 32 and this gave us some numbers.
So.
When were you clobbered over the original value of V 32?
So when assigning this my double value so we can use this printf here before my double he or let's two
before my double.
Here and then after my double.
As you can see here before, my double is almost everything is completely fine.
We got the same number, we printed the same number we assign.
And after that we just got a completely different value.
And disaster strike.
So that's the main problem with unions.
It's up to you to keep track of which interpretation is appropriate so the compiler won't help you.
You should avoid using unions in all but the rarest of cases, and you won't see them in this course
here.
Maybe in later lectures because unions has a very narrow use case in C plus plus just the daily programming.
Here.