Unions in C++.
So unions as a cousin of the P or D, that puts all of its members in the same place.
So you can think of unions as a different fiefs 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 uses related to C, C++ in operation and even packing pitfalls.
So let's create an Indian here, union here, union variant here variant.
As you can see here, this is this definition is same as struct.
So in previous lecture we define struct and create an example from it and said let's create a character.
My string here.
Ten then integer my integer here.
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 its 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 P or D, but it's completely different
under the hood here.
So let's create a variant here.
Variant V.
Here we that entered my integer for example, 32 and printf.
The answer, your answer.
Here.
We that integer?
My integer.
And let's define the v dot floating point here without my double.
My double.
Yes.
And here two point or three point here.
This is the double.
And then print printf.
My double or let's make it my integer, my union integer.
And my union double.
Here or here.
Here's my union.
Double D and.
Read out.
My double.
And then here.
Print.
My union integer.
Print here my integer two.
For example.
Your dd v dot my integer.
Actually, we have we used wrong form specified here.
We have to use f oc.
So.
Let's turn on our program now.
So let's run here.
So actually, it's the new lines here, New line.
Cops.
And you learn.
Here.
As you can see, this is the weird answer we got here.
So firstly, we declared variant free.
Next, we interpret we as an integer.
So.
And set its value to 32 here.
And then print it.
So you hear then interpret the.
As a float and risk reassign its value here.
So you printed the the to the console and it appears.
Well as you as we write 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 started to 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 here or let's
two before my double.
Here.
And then after my double.
You can see before my double is almost everything is completely fine.
We got the same number, we printed the same number of assign, and after that we just got a completely
different value.
And disaster strikes.
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
year.
Maybe in later lectures because the unions has a very narrow use case in C++.
Just the data programming.
Here.