The "this" keyword in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys my name is a Jonah and welcome back to my stables last series got a bit of a different set today hope you guys like it today we're gonna be talking all about what the this keyword is in C++ did this that's kind of hard to say so we have a keyword and C++ called this which is only accessible to us through a member function and the function meaning a function that belongs to a class so a method and inside a method we can reference this and what this is is a pointer to the current object instance that the method belongs to so of course in C++ we can write a method a non-static method and in order to call that method we need to first instantiate an object and then call the method so the method has to be called with a valid object and this the this keyword is a pointer to that object this is actually very important to how methods work in general I'm gonna be doing an in-depth videos soon link that will be help there about how how objects and classes actually work kind of internally today we're just gonna cover what this means and how we can use it and why it even exists so over here in our code I'm going to make a class called entity I'll give it a couple of public fields here X&Y and then make me a constructor which also takes in X&Y now immediately you probably say that what I want to do is assign these fields these member variables with the values of these parameters of course I could use and initialize a list and just do this and that will be totally fine that would work however if I didn't want to do that and I wanted to actually do it in the body I would be faced with a little bit of a problem because as you can probably notice they have exactly the same name so if I try and do x equals x I'm actually just assigning this X variable the parameter with its own value which is just doing absolutely nothing what I really want to do is reference the X and y that belongs to the class the actual class members and that's what the disk a what actually allows me to do so as I mentioned the keyword is basically a pointer to the current instance so to make this a little bit more clear I'm just gonna write it out here entity point so I'll just call it a and I'll assign it to this that is the type of this technically if you hover your mouse over you'll see that it's actually an NC pointer constant so you could set it to that if you want to however most people don't most people just assign it to an entity pointer because what Const means on the right-hand side of this is that when I'll allow to reassign this to something else so we couldn't write this equals null pointer for example or attempt to actually assign this to a reference here that is non constant this sense we would actually have to declare this as Const like that if you want more details on how Const works and what constantine's in that context I made a video on that which you can access by clicking the card up there so anyway back to this see what I did that if we now want to actually assign X we can just do a arrow X and just assign X or of course to make this a little bit more simple we can just do this arrow x equals x this is a pointer of course so we need to dereference it that's why we need to use the arrow alternatively we could write code like this but of course it looks a lot cleaner with the arrow so most people just do that and with Y I would do the same so now we actually have a way of assigning these two variables which is very important because we we couldn't have done it otherwise if we were to write a function which returns one of these variables for example it's pretty common for us to marketers Const because it's not going to be modifying this class so in a Const function this isn't actually equal to just an entity's actually equal to a constant I see because of course putting Const here means that we're not allowed to modify the class so this has to be Const so that we can't get away with doing stuff like this which would have been possible if we didn't do that so in summary in a Const function the actual type that you'll get if you hover your mouse over here you'll see is going to be a constant entity point a Const or just a Const entity pointer that's really the part that we care about and then if you access it inside a non-consumption is just going to be an entity pointer another useful case for this is if we maybe wanted to call a function that was outside of this entity class so it wasn't a method but we wanted to call a function outside this class from within this class that took entity as a parameter so as an example maybe we had a print entity function and because of the declaration water here I'm actually going to have to declare this up here and then define it over here so we have a function that might do some cool printing kind of stuff and we want to be able to cooperate to entity from inside this class so it may be like this I want to pass in the current instance of this entity class into this function how do I do that that's where this comes in I can just pass him this and that's going to of course passing the current instance with the X&Y that I've got set here if I wanted to take this in as a constant reference all I would have to do here is actually just dereference it and there we go so of course would be referencing this in the case of a non concert we'll just get an entity reference back like so and then a Const method we would get a Const reference back like so because this is a pointer to the current class we can also do some pretty bizarre things such as called delete this I've seen this code a handful of times in very very specialized cases my recommendation would be of course to avoid doing this because you're freeing memory from a member function and if you decide to ever access any member data after you call delete this you're going to explode because the memory had been freed so don't don't don't typically write code like this just make this is C++ there's kind of a there's a place for pretty much everything but but darn it maybe no I don't anyway I hope you guys enjoyed this episode I've actually upgraded the audio a bit few of you guys for complaining about echoing in rooms and stuff so I'm using a different microphone now hopefully it sounds better let me know what you think in the comments below if you want to support this series and basically help episodes come out quicker and be high-quality and all that you can go over to patreon Tacoma 4/2 Cherno you'll get some pretty cool rewards so just getting episodes early and being able to talk privately in a cool kind of patreon on the discord chat and all that fun stuff so definitely check that out because it does help support the series and it helps me make way more videos I will see you guys next time goodbye [Music]
Info
Channel: The Cherno
Views: 129,806
Rating: 4.9484148 out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, this keyword, c++ this, classes, object-oriented programming, programming tutorial
Id: Z_hPJ_EhceI
Channel Id: undefined
Length: 6min 8sec (368 seconds)
Published: Wed Sep 06 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.