1 00:00:00,000 --> 00:00:02,583 (bright music) 2 00:00:05,340 --> 00:00:06,173 Frank: Hello, everyone. 3 00:00:06,173 --> 00:00:07,320 Welcome back. 4 00:00:07,320 --> 00:00:10,710 I am in the Section21 workspace 5 00:00:10,710 --> 00:00:13,803 and I'm in the Stateless Lambda project. 6 00:00:14,640 --> 00:00:18,690 In this project, I've got some examples of stateless lambda 7 00:00:18,690 --> 00:00:20,370 that I'm using in different ways 8 00:00:20,370 --> 00:00:22,860 and I've got some using value parameters, 9 00:00:22,860 --> 00:00:24,990 reference parameters, passing objects, 10 00:00:24,990 --> 00:00:27,000 and returning lambdas and so forth. 11 00:00:27,000 --> 00:00:29,220 So I'll go through them one at a time. 12 00:00:29,220 --> 00:00:31,230 And I've already gone ahead and built and run. 13 00:00:31,230 --> 00:00:32,850 So you can see the run over here on the right 14 00:00:32,850 --> 00:00:34,590 and I'm just gonna walk through this. 15 00:00:34,590 --> 00:00:37,170 There are seven little test functions that I've written 16 00:00:37,170 --> 00:00:39,240 that kind of exercise a whole bunch 17 00:00:39,240 --> 00:00:41,550 of different combinations of stateless lambdas 18 00:00:41,550 --> 00:00:43,170 so you can get a pretty good feel for them. 19 00:00:43,170 --> 00:00:45,150 But before I talk about those little functions, 20 00:00:45,150 --> 00:00:46,680 let's talk about what's going on here. 21 00:00:46,680 --> 00:00:48,060 So it's important to keep in mind 22 00:00:48,060 --> 00:00:51,120 that we are working with stateless lambda expressions. 23 00:00:51,120 --> 00:00:53,340 So that means that the capture lists are empty. 24 00:00:53,340 --> 00:00:55,860 They're gonna be empty for all of the examples in here. 25 00:00:55,860 --> 00:00:58,470 You can see here that I've included iostream, 26 00:00:58,470 --> 00:00:59,340 string, and vector. 27 00:00:59,340 --> 00:01:00,600 These are just common includes 28 00:01:00,600 --> 00:01:01,890 that I'm gonna use throughout. 29 00:01:01,890 --> 00:01:03,570 I'm also including algorithm 30 00:01:03,570 --> 00:01:06,660 which gives us the standard template library algorithms. 31 00:01:06,660 --> 00:01:09,090 And here is the functional header. 32 00:01:09,090 --> 00:01:10,980 This is the one we need for stooge function 33 00:01:10,980 --> 00:01:12,030 and this is the one I mentioned 34 00:01:12,030 --> 00:01:13,920 in the slides in the previous lecture. 35 00:01:13,920 --> 00:01:15,600 So those are my includes. 36 00:01:15,600 --> 00:01:18,330 And here I've defined a real simple class person, 37 00:01:18,330 --> 00:01:19,650 and this is the one that I'm going to use 38 00:01:19,650 --> 00:01:22,320 in some of the examples where we're passing objects 39 00:01:22,320 --> 00:01:23,340 within the lambdas. 40 00:01:23,340 --> 00:01:25,320 So, here's my class Person. 41 00:01:25,320 --> 00:01:26,760 I have a friend function 42 00:01:26,760 --> 00:01:29,190 that's just my overloaded insertion operator 43 00:01:29,190 --> 00:01:32,670 that lets us display these person objects to a stream. 44 00:01:32,670 --> 00:01:34,650 And you can see it implemented down here. 45 00:01:34,650 --> 00:01:35,483 Nothing new. 46 00:01:35,483 --> 00:01:37,830 This is all stuff we have already done before. 47 00:01:37,830 --> 00:01:41,700 You can see that the person class has these two attributes, 48 00:01:41,700 --> 00:01:45,150 a name, which is a stooge string, and the person's age. 49 00:01:45,150 --> 00:01:45,983 Really simple. 50 00:01:45,983 --> 00:01:48,930 We've got a constructor that expects a name and an age. 51 00:01:48,930 --> 00:01:51,210 We've got a copy constructor. 52 00:01:51,210 --> 00:01:54,750 We've got a destructor, which I'm just letting the compiler 53 00:01:54,750 --> 00:01:56,040 create the default for me 54 00:01:56,040 --> 00:01:58,230 since there's nothing special to do here. 55 00:01:58,230 --> 00:02:00,210 And then right here I've got my getters 56 00:02:00,210 --> 00:02:01,830 and my setters, right? 57 00:02:01,830 --> 00:02:04,780 I've got my get_name, set_name, get_age, set_age. 58 00:02:04,780 --> 00:02:06,990 Okay, so now that that's outta the way, 59 00:02:06,990 --> 00:02:08,460 you can see it's really straightforward. 60 00:02:08,460 --> 00:02:10,530 I'll refer back to this when we get to it. 61 00:02:10,530 --> 00:02:12,810 You can see down here in my main 62 00:02:12,810 --> 00:02:15,540 what I'm doing is I'm calling these functions 63 00:02:15,540 --> 00:02:18,480 all the way from test one all the way to test seven. 64 00:02:18,480 --> 00:02:20,580 So I'm gonna walk through them one at a time. 65 00:02:20,580 --> 00:02:22,890 And each one adds a little bit and changes a little bit 66 00:02:22,890 --> 00:02:25,860 just so you can get a good feel for the differences 67 00:02:25,860 --> 00:02:27,840 between the stateless lambda expressions 68 00:02:27,840 --> 00:02:29,130 that I'm gonna show you. 69 00:02:29,130 --> 00:02:31,170 All right, so let me start with test one. 70 00:02:31,170 --> 00:02:33,360 I'm just gonna scroll up to it. 71 00:02:33,360 --> 00:02:35,340 And here is test one. 72 00:02:35,340 --> 00:02:38,940 And you can see over here in the output right up here, 73 00:02:38,940 --> 00:02:40,710 that's the output for test one. 74 00:02:40,710 --> 00:02:43,320 So that's what I'll be referring to as I walk through this. 75 00:02:43,320 --> 00:02:44,250 Real simple. 76 00:02:44,250 --> 00:02:46,530 We have no capture list. 77 00:02:46,530 --> 00:02:48,450 None of these guys are capturing anything 78 00:02:48,450 --> 00:02:49,830 that's why they're stateless. 79 00:02:49,830 --> 00:02:52,860 We have no parameters. 80 00:02:52,860 --> 00:02:56,910 And the body of the lambda just says cout Hi in this case. 81 00:02:56,910 --> 00:02:58,503 So it's just gonna display Hi. 82 00:02:59,580 --> 00:03:01,620 We are instantiating it right there. 83 00:03:01,620 --> 00:03:03,330 It's really important that we use that. 84 00:03:03,330 --> 00:03:05,613 Otherwise, it's not doing anything. 85 00:03:06,450 --> 00:03:10,560 At this point now ,that lambda expression will execute 86 00:03:10,560 --> 00:03:12,210 and Hi is displayed. 87 00:03:12,210 --> 00:03:13,470 You can see it right there. 88 00:03:13,470 --> 00:03:15,390 Same idea with this guy here 89 00:03:15,390 --> 00:03:17,610 except now we've got a parameter. 90 00:03:17,610 --> 00:03:19,590 We've got an integer parameter x. 91 00:03:19,590 --> 00:03:21,450 Again, stateless, right? 92 00:03:21,450 --> 00:03:26,040 So this is our parameter x and we are displaying x. 93 00:03:26,040 --> 00:03:27,840 That's the body of the lambda. 94 00:03:27,840 --> 00:03:31,860 So whatever value is in here will be displayed here 95 00:03:31,860 --> 00:03:34,140 when we display the output statement. 96 00:03:34,140 --> 00:03:35,940 Now, same idea over here. 97 00:03:35,940 --> 00:03:39,813 We're calling that lambda and we're passing in 100. 98 00:03:39,813 --> 00:03:44,460 This 100 is being passed in here, right? 99 00:03:44,460 --> 00:03:46,080 So now x gets the 100. 100 00:03:46,080 --> 00:03:49,290 And when this body executes, we're going to display 100, 101 00:03:49,290 --> 00:03:51,990 and that's exactly what's happening here. 102 00:03:51,990 --> 00:03:54,510 All right, so let's look at the third example. 103 00:03:54,510 --> 00:03:57,330 Same idea, except now I've got two parameters. 104 00:03:57,330 --> 00:03:58,770 I've got an x and a y. 105 00:03:58,770 --> 00:04:01,350 So we're displaying the x plus the y over here. 106 00:04:01,350 --> 00:04:02,550 We're just adding them up. 107 00:04:02,550 --> 00:04:03,870 We're displaying the sum. 108 00:04:03,870 --> 00:04:07,350 You see the 100 is being passed over here to the x, 109 00:04:07,350 --> 00:04:09,750 the 200 is being passed into the y. 110 00:04:09,750 --> 00:04:12,060 All this is happening by value, right? 111 00:04:12,060 --> 00:04:13,410 There's no references in here. 112 00:04:13,410 --> 00:04:14,640 This is by value. 113 00:04:14,640 --> 00:04:16,410 So we're doing copies. 114 00:04:16,410 --> 00:04:18,777 And what we're gonna do is 100 plus 200, 115 00:04:18,777 --> 00:04:21,120 and this guy will display 300, 116 00:04:21,120 --> 00:04:23,170 and that's what you're seeing right here. 117 00:04:24,210 --> 00:04:27,420 Okay, so that's a real, real simple example 118 00:04:27,420 --> 00:04:28,863 of lambda expressions. 119 00:04:30,360 --> 00:04:31,800 Simple but not very useful, right? 120 00:04:31,800 --> 00:04:33,300 You can see it's kind of silly. 121 00:04:33,300 --> 00:04:34,410 Why would you do this, you know? 122 00:04:34,410 --> 00:04:35,940 Can't we just it? 123 00:04:35,940 --> 00:04:38,160 It's kind of contrived if you will, 124 00:04:38,160 --> 00:04:40,830 and nobody really writes lambda expressions this way. 125 00:04:40,830 --> 00:04:42,540 It's more what I'm gonna show you in a little bit. 126 00:04:42,540 --> 00:04:45,150 But this gives you a good feeling for the structure 127 00:04:45,150 --> 00:04:47,730 and the syntax of the lambda expressions. 128 00:04:47,730 --> 00:04:49,350 They're not complicated. 129 00:04:49,350 --> 00:04:50,640 You've got the capture list. 130 00:04:50,640 --> 00:04:52,170 You've got the parameter list. 131 00:04:52,170 --> 00:04:53,760 You've got the body of the lambda. 132 00:04:53,760 --> 00:04:55,350 That's what it's like. 133 00:04:55,350 --> 00:04:57,900 It can get more complicated when we're doing captures, 134 00:04:57,900 --> 00:04:59,640 and we'll do that in the next video. 135 00:04:59,640 --> 00:05:02,550 Okay, so let me show you now test two. 136 00:05:02,550 --> 00:05:04,410 Let me close this piece down. 137 00:05:04,410 --> 00:05:05,670 And here I've got test two. 138 00:05:05,670 --> 00:05:07,740 And you can see here what I'm doing 139 00:05:07,740 --> 00:05:11,040 is using values and references now for the parameters. 140 00:05:11,040 --> 00:05:12,210 So it's gonna be a little bit different 141 00:05:12,210 --> 00:05:14,550 because we're gonna be using reference parameters. 142 00:05:14,550 --> 00:05:17,883 In this case, I've got a variable here. 143 00:05:18,930 --> 00:05:21,810 What I'm doing over here is I've got a lambda expression 144 00:05:21,810 --> 00:05:24,840 and I'm assigning that lambda expression to a variable. 145 00:05:24,840 --> 00:05:26,610 This is the same thing as saying something 146 00:05:26,610 --> 00:05:30,360 like an x equals, you know, three plus five. 147 00:05:30,360 --> 00:05:34,470 I'm evaluating that expression and assigning it to x. 148 00:05:34,470 --> 00:05:37,380 Later on, when I use x, I'm gonna be using eight, right? 149 00:05:37,380 --> 00:05:39,420 This is exactly the same idea. 150 00:05:39,420 --> 00:05:42,090 This is a variable l1. 151 00:05:42,090 --> 00:05:44,430 What's important here is I'm using the auto keyword 152 00:05:44,430 --> 00:05:46,500 to tell the compiler, hey, compiler, 153 00:05:46,500 --> 00:05:48,480 you figure out what the type of this thing is. 154 00:05:48,480 --> 00:05:50,550 It's a lambda, it's not an ant, it's not a void, 155 00:05:50,550 --> 00:05:52,290 it's not a double, it's a lambda. 156 00:05:52,290 --> 00:05:54,750 Let the compiler figure out how to do it. 157 00:05:54,750 --> 00:05:56,940 Same idea, it's the same lambda as before. 158 00:05:56,940 --> 00:05:57,900 Notice the difference though. 159 00:05:57,900 --> 00:06:00,300 We don't have that piece at the end, right? 160 00:06:00,300 --> 00:06:03,930 So what we're doing is we're creating that lambda expression 161 00:06:03,930 --> 00:06:06,600 and we're assigning it to l1. 162 00:06:06,600 --> 00:06:08,850 Now, whenever I want to call it, 163 00:06:08,850 --> 00:06:12,090 now I could just say l1 with the perens. 164 00:06:12,090 --> 00:06:15,120 It looks just like a function call and essentially it is. 165 00:06:15,120 --> 00:06:16,860 Remember we talked about overloading 166 00:06:16,860 --> 00:06:18,810 the function call operator? 167 00:06:18,810 --> 00:06:21,030 Well, that's really what's happening behind the scenes. 168 00:06:21,030 --> 00:06:22,680 So at this point right here, 169 00:06:22,680 --> 00:06:26,340 not here, but right there on line 45, 170 00:06:26,340 --> 00:06:28,140 that's when we get the Hi here. 171 00:06:28,140 --> 00:06:29,670 So hopefully that's clear. 172 00:06:29,670 --> 00:06:32,700 And in this case now, let's look at lines 47 and 48. 173 00:06:32,700 --> 00:06:34,917 We have two integers, num1 and num2 174 00:06:34,917 --> 00:06:38,310 and each one has been initialized to 100. 175 00:06:38,310 --> 00:06:41,710 I'm creating another, variable l2 right here 176 00:06:42,570 --> 00:06:44,460 which is also a lambda, right? 177 00:06:44,460 --> 00:06:45,870 Again, stateless. 178 00:06:45,870 --> 00:06:48,420 I've got two parameters by value 179 00:06:48,420 --> 00:06:50,250 and I'm doing the same thing, x plus y, 180 00:06:50,250 --> 00:06:51,840 same example I used earlier. 181 00:06:51,840 --> 00:06:53,400 But now look what I'm doing. 182 00:06:53,400 --> 00:06:54,233 I'm calling it. 183 00:06:54,233 --> 00:06:55,317 So I'm calling, I'm pass l2 184 00:06:55,317 --> 00:06:57,197 and then I'm passing a 10 and a 20. 185 00:06:57,197 --> 00:07:00,630 So the 10 gets over here and the 20 comes over here. 186 00:07:00,630 --> 00:07:04,110 This guy's gonna be 30 and that's what's gonna display. 187 00:07:04,110 --> 00:07:08,190 Right here, we get that 30 displayed. 188 00:07:08,190 --> 00:07:10,440 Now, those are two integer literals 189 00:07:10,440 --> 00:07:12,330 and that's perfectly valid. 190 00:07:12,330 --> 00:07:15,300 In this case, we've got num1, num2. 191 00:07:15,300 --> 00:07:16,470 So now we're gonna pass in, 192 00:07:16,470 --> 00:07:19,770 instead of 10 and 20, 100 and 100, right? 193 00:07:19,770 --> 00:07:21,630 So we're gonna display 200 194 00:07:21,630 --> 00:07:24,180 which is what's being displayed here. 195 00:07:24,180 --> 00:07:27,690 Remember, this is passed by value, this is value parameters, 196 00:07:27,690 --> 00:07:30,840 so we're making copies of these values. 197 00:07:30,840 --> 00:07:34,260 So now let's take a look at an example with some references. 198 00:07:34,260 --> 00:07:37,950 And now we are right here on line 54 199 00:07:37,950 --> 00:07:39,240 and we've got the same idea. 200 00:07:39,240 --> 00:07:44,240 I've got a variable, l3, and we've got two parameters now. 201 00:07:44,580 --> 00:07:48,180 The difference is now x is a reference parameter. 202 00:07:48,180 --> 00:07:49,230 We know what those are, right? 203 00:07:49,230 --> 00:07:51,150 We've talked about 'em already in the class. 204 00:07:51,150 --> 00:07:54,630 It's basically an alias to an actual parameter. 205 00:07:54,630 --> 00:07:58,650 And I have y, which is a value type parameter. 206 00:07:58,650 --> 00:07:59,820 And all this lambda does, 207 00:07:59,820 --> 00:08:03,480 it's gonna display x and then it's gonna display y. 208 00:08:03,480 --> 00:08:07,110 But within the body of the lambda, we're changing x and y. 209 00:08:07,110 --> 00:08:09,390 Now, this is really important to understand. 210 00:08:09,390 --> 00:08:12,360 When I'm changing x, I've got a reference. 211 00:08:12,360 --> 00:08:15,540 So that means that the actual parameter 212 00:08:15,540 --> 00:08:18,630 is going to be changed to a 1,000. 213 00:08:18,630 --> 00:08:20,520 When I change y to 2,000, 214 00:08:20,520 --> 00:08:24,720 the local copy of y will be changed, so not the actual. 215 00:08:24,720 --> 00:08:27,360 So let me show you what that looks like when I call this. 216 00:08:27,360 --> 00:08:28,740 So at this point, 217 00:08:28,740 --> 00:08:31,551 remember we're gonna call this with num1 and num2. 218 00:08:31,551 --> 00:08:34,169 And if you remember, num1 and num2 are 100 each, right? 219 00:08:34,169 --> 00:08:36,210 So what's gonna happen is, actually, let me clear this up 220 00:08:36,210 --> 00:08:38,490 and do this one more time so it's clearer. 221 00:08:38,490 --> 00:08:41,669 I've got num1 and num2 which is each 100. 222 00:08:41,669 --> 00:08:45,725 Now I'm gonna call l3 with num1 and num2. 223 00:08:45,725 --> 00:08:47,490 num2 is a 100. 224 00:08:47,490 --> 00:08:52,140 So that becomes the y and that's copied. 225 00:08:52,140 --> 00:08:54,450 But num1 is a reference, right? 226 00:08:54,450 --> 00:08:57,660 So what happens is this guy now 227 00:08:57,660 --> 00:09:01,440 refers to the actual parameter up here. 228 00:09:01,440 --> 00:09:04,200 This guy's just local, okay? 229 00:09:04,200 --> 00:09:05,190 That's really important. 230 00:09:05,190 --> 00:09:06,570 It's a very important distinction. 231 00:09:06,570 --> 00:09:08,880 We learned about this when we used functions. 232 00:09:08,880 --> 00:09:10,740 It's exactly the same thing. 233 00:09:10,740 --> 00:09:13,380 So at this point, we're gonna execute this code. 234 00:09:13,380 --> 00:09:14,730 This code is going to change, 235 00:09:14,730 --> 00:09:16,740 and first of all, it's gonna display x and y 236 00:09:16,740 --> 00:09:18,166 which are 100 and 100, right, 237 00:09:18,166 --> 00:09:19,080 'cause I haven't changed them. 238 00:09:19,080 --> 00:09:21,180 You can see it right there. 239 00:09:21,180 --> 00:09:23,550 Then it's gonna change x to 1,000. 240 00:09:23,550 --> 00:09:24,810 That's this guy right here. 241 00:09:24,810 --> 00:09:27,090 So this guy's gonna be 1,000. 242 00:09:27,090 --> 00:09:30,300 it's gonna change Y to 2,000, which is this guy. 243 00:09:30,300 --> 00:09:31,920 It's irrelevant 'cause that's gonna go away 244 00:09:31,920 --> 00:09:33,420 when the lambda is finished. 245 00:09:33,420 --> 00:09:34,740 Now we're done. 246 00:09:34,740 --> 00:09:36,990 So what happens is all this gets cleaned up 247 00:09:36,990 --> 00:09:38,340 and the only thing that's left behind 248 00:09:38,340 --> 00:09:42,870 is this guy's 1,000 num1, is 1,000, num2 is 100. 249 00:09:42,870 --> 00:09:44,490 So when we come back, 250 00:09:44,490 --> 00:09:46,647 we're just gonna display num1 and num2. 251 00:09:46,647 --> 00:09:49,470 And you can see right here we're getting the 1,000, 252 00:09:49,470 --> 00:09:51,273 which we changed, and the 100. 253 00:09:52,590 --> 00:09:53,970 Okay, hopefully that's pretty clear. 254 00:09:53,970 --> 00:09:56,190 If not, just walk through it yourself just like I did 255 00:09:56,190 --> 00:09:57,840 and that way you'll get a really, really good feeling 256 00:09:57,840 --> 00:09:59,070 of what's going on here. 257 00:09:59,070 --> 00:10:01,950 Okay, so let's move on to test three. 258 00:10:01,950 --> 00:10:04,770 All right, so what are we doing in test three? 259 00:10:04,770 --> 00:10:07,320 We're basically doing the same thing we did in test two 260 00:10:07,320 --> 00:10:12,320 except we're using objects now instead of primitive types. 261 00:10:12,600 --> 00:10:14,430 The other example we used integers. 262 00:10:14,430 --> 00:10:16,260 So what's going on here? 263 00:10:16,260 --> 00:10:20,250 Well, I'm gonna create a person object called stooge, 264 00:10:20,250 --> 00:10:22,920 and that would be Larry and Larry's 18 years old. 265 00:10:22,920 --> 00:10:25,530 And then I want to display stooge here. 266 00:10:25,530 --> 00:10:28,380 So right on line 68, I'm in test three, 267 00:10:28,380 --> 00:10:30,240 this is what's happening right there. 268 00:10:30,240 --> 00:10:31,953 That one line is executing. 269 00:10:33,150 --> 00:10:35,460 Larry 18, just what we expect. 270 00:10:35,460 --> 00:10:39,120 Now I'm creating a lambda, I'll call it l4. 271 00:10:39,120 --> 00:10:43,650 In this example here, we are passing in a person object. 272 00:10:43,650 --> 00:10:45,240 This is really important. 273 00:10:45,240 --> 00:10:47,073 This is by value. 274 00:10:48,000 --> 00:10:50,640 So what happens here, a copy. 275 00:10:50,640 --> 00:10:52,440 And how do we copy objects? 276 00:10:52,440 --> 00:10:54,093 Copy constructor. 277 00:10:55,380 --> 00:10:57,270 The copy constructor will be called 278 00:10:57,270 --> 00:11:00,060 and it'll create a new copy of that object. 279 00:11:00,060 --> 00:11:02,430 That copy of the object will be alive. 280 00:11:02,430 --> 00:11:03,900 Within the body of the lambda, 281 00:11:03,900 --> 00:11:05,220 we can change it all we want in there 282 00:11:05,220 --> 00:11:07,320 and it's not gonna affect the actual. 283 00:11:07,320 --> 00:11:08,340 That's really important. 284 00:11:08,340 --> 00:11:10,350 Same thing that just happened with integers, 285 00:11:10,350 --> 00:11:12,240 but now we're dealing with objects. 286 00:11:12,240 --> 00:11:15,633 So now what am I passing into l4 stooge. 287 00:11:16,500 --> 00:11:19,830 So now what's happening is I get a new version, right, 288 00:11:19,830 --> 00:11:22,320 a new copy of that stooge object. 289 00:11:22,320 --> 00:11:25,470 So inside this function here I have p, 290 00:11:25,470 --> 00:11:29,040 and p would be Larry 18, 291 00:11:29,040 --> 00:11:31,710 which is totally separate from this Larry 18. 292 00:11:31,710 --> 00:11:32,943 This is a new copy. 293 00:11:33,870 --> 00:11:36,180 And that's active, so I'm gonna display it 294 00:11:36,180 --> 00:11:38,100 and it's gonna display right there. 295 00:11:38,100 --> 00:11:39,810 That's the second display statement, right? 296 00:11:39,810 --> 00:11:41,223 The first one was up here. 297 00:11:42,810 --> 00:11:43,643 And that's it. 298 00:11:43,643 --> 00:11:45,390 Stooge did not change, right? 299 00:11:45,390 --> 00:11:48,420 It can't change because we made a copy of it. 300 00:11:48,420 --> 00:11:51,020 Now let's do it by reference and see the difference. 301 00:11:52,410 --> 00:11:54,450 I'm right here on line 75. 302 00:11:54,450 --> 00:11:55,980 Remember we've already executed that one 303 00:11:55,980 --> 00:11:58,290 and we've executed that statement right over here. 304 00:11:58,290 --> 00:11:59,123 So I'm over here. 305 00:11:59,123 --> 00:12:04,123 l5, again stateless, one parameter, a reference parameter. 306 00:12:04,680 --> 00:12:06,540 Really, really different now, right? 307 00:12:06,540 --> 00:12:08,670 No copy constructor, no copy. 308 00:12:08,670 --> 00:12:10,200 We're gonna get an alias. 309 00:12:10,200 --> 00:12:12,450 At this point, we also said it's constant. 310 00:12:12,450 --> 00:12:16,230 So if we try to change that p object in here in the body, 311 00:12:16,230 --> 00:12:17,063 we won't be allowed. 312 00:12:17,063 --> 00:12:18,900 The compiler will give us an error. 313 00:12:18,900 --> 00:12:21,150 So let's do this, l5. 314 00:12:21,150 --> 00:12:22,860 We're gonna pass in stooge. 315 00:12:22,860 --> 00:12:24,540 We don't make a copy of it. 316 00:12:24,540 --> 00:12:28,500 This p refers to the real one. 317 00:12:28,500 --> 00:12:30,060 So again, very efficient. 318 00:12:30,060 --> 00:12:31,800 I can't change it because it's cons, 319 00:12:31,800 --> 00:12:33,930 so I get the best of both worlds, right? 320 00:12:33,930 --> 00:12:36,240 When I'm in here, I'm displaying p. 321 00:12:36,240 --> 00:12:38,730 p is right there, Larry 18. 322 00:12:38,730 --> 00:12:42,780 And you can see that third statement executes right there. 323 00:12:42,780 --> 00:12:44,460 So that's passed by reference. 324 00:12:44,460 --> 00:12:46,050 Now, let's do pass by reference 325 00:12:46,050 --> 00:12:47,850 but actually modify the object, 326 00:12:47,850 --> 00:12:51,060 and that's what this last statement here does. 327 00:12:51,060 --> 00:12:52,350 Let me scroll up just a little bit 328 00:12:52,350 --> 00:12:53,850 so you can see a little bit better. 329 00:12:53,850 --> 00:12:57,270 So remember at this point we've done this, this, this 330 00:12:57,270 --> 00:12:59,493 and we are right here now. 331 00:13:00,480 --> 00:13:03,360 So my lambda variable is l6. 332 00:13:03,360 --> 00:13:07,590 I'm passing in a person object by reference. 333 00:13:07,590 --> 00:13:09,930 Really important, notice there's no cons over here 334 00:13:09,930 --> 00:13:11,010 like there was over here. 335 00:13:11,010 --> 00:13:13,500 I'm able to modify that and I'm gonna modify that. 336 00:13:13,500 --> 00:13:15,570 So I'm gonna pass in stooge. 337 00:13:15,570 --> 00:13:17,940 This p will be stooge, right? 338 00:13:17,940 --> 00:13:20,310 So it's going to be a reference to stooge 339 00:13:20,310 --> 00:13:21,240 and it's defined up there. 340 00:13:21,240 --> 00:13:26,240 It's scrolled off, but it was stooge and it was Larry 18. 341 00:13:26,460 --> 00:13:27,693 That's my object. 342 00:13:28,830 --> 00:13:30,240 But now look what I'm doing in the body. 343 00:13:30,240 --> 00:13:33,300 I'm saying, hey, set the name to Frank. 344 00:13:33,300 --> 00:13:38,300 So I'm changing Larry to Frank and change the age to 25. 345 00:13:38,370 --> 00:13:40,503 So I'm changing the 18 to a 25. 346 00:13:41,340 --> 00:13:43,560 Then I'm displaying p. 347 00:13:43,560 --> 00:13:46,350 Well, p is a reference to this guy, right? 348 00:13:46,350 --> 00:13:48,210 And notice right there, what happens? 349 00:13:48,210 --> 00:13:50,130 Frank 25. 350 00:13:50,130 --> 00:13:53,760 Then once I finish, I'm gonna display stooge now. 351 00:13:53,760 --> 00:13:55,140 Well, that is stooge, right? 352 00:13:55,140 --> 00:13:56,340 So I'm gonna get the same thing. 353 00:13:56,340 --> 00:13:59,010 So there's proof here, right, in both cases. 354 00:13:59,010 --> 00:14:01,830 There's the proof that we actually changed 355 00:14:01,830 --> 00:14:04,890 that stooge variable out there from within the lambda 356 00:14:04,890 --> 00:14:07,380 because of that reference parameter. 357 00:14:07,380 --> 00:14:09,450 Okay, so this video's getting a little bit long now. 358 00:14:09,450 --> 00:14:11,250 So what I'm gonna do is I'm gonna stop this video here 359 00:14:11,250 --> 00:14:14,160 and we'll continue with test four in the next video. 360 00:14:14,160 --> 00:14:15,110 I'll see you there.