hey what's going on folks it's Mike here and Welcome to our next lesson in the modern C plus plus Series in this lesson we're going to continue talking about Lambda so if you haven't seen the previous lessons make sure to check out the course playlist in the description below so you can follow along but with that said let's go ahead and take a look at how do we use lambdas with objects so in member functions for instance if we want to capture this so with that said let's just go ahead and take a look at an example so you understand exactly what's going on here now what I'm going to go ahead and do here is just create a new structure here with a member variable and initialize an instance of it now let's go ahead and play around with this struct a little bit and create a function here I'm just going to call it very conveniently function here and we'll go ahead and make use of the counter variable here and let's just go ahead and say what our counter is and maybe put an end line at the end here and then maybe call this function a few times on our specific instance here so our instance we'll just call function here and let's just go ahead and call it I don't know four times just to demonstrate the purpose in this lesson of what's going on here so go ahead and compile this I'll use the bug symbols warnings and treat things as errors just for completion it's a good habit to have and let's go ahead and run it and obviously we're not modifying this counter in any way inside of our actual instance uh in this member function here from our class okay so with that said here let's go ahead and use a Lambda within this member function something that you might want to do for instance if you're modifying a function or using anything inside of lib algorithm for instance so let's go ahead and just create something I'm just going to call it f here for griffity here we'll have a Lambda function and we want to go ahead and do something with counter now again let's just go ahead and see if I can print out what counter is here I'm just going to say counter inside uh Lambda and let's go ahead and spell this out here and I'll put a colon here and let's just go ahead and try to print out the counter again and let's go ahead and compile this well now you're going to get an error here and this is what today's lesson is all about here this error message here saying this was not captured for this Lambda function so if I go ahead and bring in CPP reference here our favorite website here I'm going to go ahead down to uh the Lambda capture on the Lambda page here and it is showing that well if we want to capture for the current instance of the object this that's what we pass into the capture okay so it's actually as simple as that so we can actually see some of these rules that we've covered in the previous lessons here as well of the different types of things that we want to actually capture and going down to listing number seven which I'm going to make just a little bit bigger so folks can see if you're watching this on a phone for instance that this is a simple bi-reference capture of the current object and then we can also do a simple by copy capture of the object okay so let's go ahead and look at what this means here and then we'll go ahead and see uh how exactly this is useful okay so what I want to do here in the capture is the current instance of this object I want to be able to capture its member variables so let's go ahead and try to recompile this again and well it looks like it's working here but our compiler is smart enough to say well we're not using this variable F so let's go ahead and use it again I like using warnings as errors but we should go ahead and call our function here for our Lambda so we can demonstrate the point of this video okay so we see that's working here and now I can actually run this program here and we'll see counter inside of Lambda is zero the counter outside is zero as well okay so now that we know that we can access the counter here and we're doing this by reference again that was what it says here in listing number seven simple by reference capture of the current object we should be able to modify the counter variable here and that should be able to uh modify counter well inside of this Lambda function so it should report an update here as well as inside of our class as well so let's go ahead and recompile this rerun it and we can see likewise we get one one two two and so on so we're actually capturing the changes for this instance of the object from this Lambda function okay so that's great here now let's go ahead and look at the other way that we can capture which is by value here and why you might want to do this again usually has to relate to say if you're doing concurrent programming and sometimes you don't want to pass a bunch of things by reference right you want actual copies so let's just go ahead and pass in this here and I'm not going to change anything here but let's go ahead and see if you can predict what the behavior is and the behavior is error because remember for passing something in uh by value here and I'm trying to modify it it's read only by default you can go ahead and watch the previous lessons where we push this into to CPP insights and you'll actually see that this is a const function so if we want to modify this we could do something like doing mutable for instance and that should allow us to now modify the actual variable within the Lambda function but again we're modifying a copy here so it's not actually changing the counter here when we're passing it by value so just again keep that in mind you can review the previous lessons if you need to refresh on that now because we're able to just pass by um reference with just this here and I'll go ahead and get rid of this again that was modifying the actual code here and something that you will see folks do sometimes is you'll see them say something like pointer equals this or objects equals this and then you can actually do object something like this here um oops let's go ahead and uh go ahead and since I'm using uh object here uh I need to access it like so um then you can actually refer to the sort of this pointer here and sometimes it's helpful to give it you know name explicitly again to deal with uh issues of ambiguity if you have variables or other things that might be the same name so that's just nice little thing you could do here um with your capture if you want all right and I will note that you know this idea of using uh some of the things that we've learned about let's go ahead and Rewind and just put the Ampersand which captures everything by reference I'm going to go back here uh and again this is recapping or reviewing some of the other things that we learned about uh works just fine as shown here but we did learn about something like doing equal which would capture everything by value here and this has actually been a deprecated feature in C plus plus um 20 here as the error message is giving us I mean we can go ahead and try if I comment sent out the thing that I'm removing here we can go ahead and try to compile this in say C plus plus 17. let's go ahead and see that this will work it lets us do that but that was that actually functionality that was removed so again I would encourage you to compile with the latest version that is possible and do 20 here now just in case you do need to copy uh by value here let's go ahead and go back to 17 here uh and we learned that we could do that with uh let's see Star this let's go ahead and see if that works well that will work here so if you do need to do that um again be explicit I guess about that you want that actual instance of the object that you're working in to be uh copied here so go ahead and compile this I think that'll be less of an issue if you're following along with this series or if you have control of your code base so again just use 20 um to you don't have to worry about some of those uh deprecated features here all right folks so I hope that shows you something useful for how to capture uh the actual object that you're working with or the instance of object now you can use lambdas within your code inside of your classes if you like here so with that said folks thank you for joining me for this lesson and continuing on with the Lambda series and the modern C plus series here if you haven't already make sure you subscribe so that you don't miss any of these lessons that are coming out and comment below if there's something else that you want to see some other cool feature of lambdis for instance that you want to learn about or maybe you know about that you want to share with the rest of the community as always thanks for your time and I'll look forward to seeing you in the next lesson
Get free YouTube transcripts with timestamps, translation, and download options.
Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact