1 00:00:01,470 --> 00:00:03,630 Unions in C++. 2 00:00:03,810 --> 00:00:11,580 So unions as a cousin of the P or D, that puts all of its members in the same place. 3 00:00:11,850 --> 00:00:20,190 So you can think of unions as a different fiefs or interpretations of a block of a memory so they can 4 00:00:20,190 --> 00:00:26,850 be useful in some low level situations, such as when marshalling structures that must be consistent 5 00:00:26,850 --> 00:00:27,660 across architectures. 6 00:00:27,660 --> 00:00:36,870 So dealing with the type checking uses related to C, C++ in operation and even packing pitfalls. 7 00:00:36,870 --> 00:00:44,310 So let's create an Indian here, union here, union variant here variant. 8 00:00:45,880 --> 00:00:49,510 As you can see here, this is this definition is same as struct. 9 00:00:49,510 --> 00:00:57,310 So in previous lecture we define struct and create an example from it and said let's create a character. 10 00:00:57,310 --> 00:00:59,980 My string here. 11 00:01:00,130 --> 00:01:06,160 Ten then integer my integer here. 12 00:01:07,920 --> 00:01:08,760 Double. 13 00:01:09,650 --> 00:01:15,110 Double my my double, for example. 14 00:01:16,360 --> 00:01:27,990 So then the union here variant can be interpreted as my string ten an integer or a double. 15 00:01:28,440 --> 00:01:32,880 So it takes up only as much memory as its largest member. 16 00:01:32,880 --> 00:01:36,690 Probably a string here, my string in this case. 17 00:01:36,690 --> 00:01:43,480 So you can use that operator to specify a union's interpretation. 18 00:01:43,500 --> 00:01:51,240 Syntactically, this looks like this looks like accessing a member of P or D, but it's completely different 19 00:01:51,240 --> 00:01:52,220 under the hood here. 20 00:01:52,230 --> 00:01:55,620 So let's create a variant here. 21 00:01:55,830 --> 00:01:57,660 Variant V. 22 00:01:59,260 --> 00:02:07,680 Here we that entered my integer for example, 32 and printf. 23 00:02:08,740 --> 00:02:13,210 The answer, your answer. 24 00:02:14,070 --> 00:02:14,590 Here. 25 00:02:15,950 --> 00:02:18,710 We that integer? 26 00:02:18,860 --> 00:02:19,970 My integer. 27 00:02:19,970 --> 00:02:26,390 And let's define the v dot floating point here without my double. 28 00:02:26,420 --> 00:02:27,990 My double. 29 00:02:28,010 --> 00:02:28,580 Yes. 30 00:02:28,940 --> 00:02:35,480 And here two point or three point here. 31 00:02:35,510 --> 00:02:36,950 This is the double. 32 00:02:37,550 --> 00:02:39,140 And then print printf. 33 00:02:42,680 --> 00:02:50,120 My double or let's make it my integer, my union integer. 34 00:02:51,110 --> 00:02:55,520 And my union double. 35 00:02:56,510 --> 00:02:58,730 Here or here. 36 00:02:59,660 --> 00:03:00,680 Here's my union. 37 00:03:00,680 --> 00:03:03,650 Double D and. 38 00:03:06,380 --> 00:03:07,520 Read out. 39 00:03:10,310 --> 00:03:11,240 My double. 40 00:03:13,040 --> 00:03:14,630 And then here. 41 00:03:17,240 --> 00:03:17,570 Print. 42 00:03:20,330 --> 00:03:23,660 My union integer. 43 00:03:25,850 --> 00:03:28,720 Print here my integer two. 44 00:03:28,730 --> 00:03:29,630 For example. 45 00:03:30,290 --> 00:03:34,950 Your dd v dot my integer. 46 00:03:34,970 --> 00:03:38,510 Actually, we have we used wrong form specified here. 47 00:03:38,510 --> 00:03:41,660 We have to use f oc. 48 00:03:42,350 --> 00:03:43,250 So. 49 00:03:45,770 --> 00:03:47,570 Let's turn on our program now. 50 00:03:48,020 --> 00:03:50,450 So let's run here. 51 00:03:52,030 --> 00:03:57,400 So actually, it's the new lines here, New line. 52 00:03:59,490 --> 00:04:00,140 Cops. 53 00:04:03,920 --> 00:04:04,980 And you learn. 54 00:04:04,980 --> 00:04:05,790 And you learn. 55 00:04:06,830 --> 00:04:07,340 Here. 56 00:04:08,930 --> 00:04:13,340 As you can see, this is the weird answer we got here. 57 00:04:13,400 --> 00:04:17,510 So firstly, we declared variant free. 58 00:04:18,050 --> 00:04:22,940 Next, we interpret we as an integer. 59 00:04:23,180 --> 00:04:23,660 So. 60 00:04:23,660 --> 00:04:26,870 And set its value to 32 here. 61 00:04:27,800 --> 00:04:29,330 And then print it. 62 00:04:30,450 --> 00:04:34,680 So you hear then interpret the. 63 00:04:35,570 --> 00:04:40,700 As a float and risk reassign its value here. 64 00:04:43,640 --> 00:04:48,500 So you printed the the to the console and it appears. 65 00:04:48,510 --> 00:04:51,630 Well as you as we write it. 66 00:04:51,950 --> 00:04:53,230 So far so good here. 67 00:04:53,240 --> 00:05:01,940 So disaster strikes only when you try to interrupt V as an integer again here. 68 00:05:02,700 --> 00:05:07,860 As you can see, our integer started to and this gave us some numbers. 69 00:05:08,160 --> 00:05:09,150 So. 70 00:05:10,330 --> 00:05:15,410 When were you clobbered over the original value of V 32? 71 00:05:15,430 --> 00:05:25,570 So when assigning this my double value so we can use this printf here before my double here or let's 72 00:05:25,600 --> 00:05:28,870 two before my double. 73 00:05:29,710 --> 00:05:30,080 Here. 74 00:05:30,880 --> 00:05:34,270 And then after my double. 75 00:05:37,240 --> 00:05:42,900 You can see before my double is almost everything is completely fine. 76 00:05:42,920 --> 00:05:50,510 We got the same number, we printed the same number of assign, and after that we just got a completely 77 00:05:50,510 --> 00:05:51,350 different value. 78 00:05:52,300 --> 00:05:53,800 And disaster strikes. 79 00:05:54,010 --> 00:05:56,440 So that's the main problem with unions. 80 00:05:56,470 --> 00:06:02,950 It's up to you to keep track of which interpretation is appropriate so the compiler won't help you. 81 00:06:03,280 --> 00:06:10,900 You should avoid using unions in all but the rarest of cases, and you won't see them in this course 82 00:06:10,900 --> 00:06:11,380 year. 83 00:06:12,850 --> 00:06:20,110 Maybe in later lectures because the unions has a very narrow use case in C++. 84 00:06:20,110 --> 00:06:21,490 Just the data programming. 85 00:06:26,170 --> 00:06:26,680 Here.