REFERENCES in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys my name is DeSean oh and welcome back to my C++ series today we're going to talking about references last week we talked about pointers so if you didn't see that video you are 100% going to have to because reference is a really just an extension of pointers so you need to be able to understand at least at a basic level how pointers work in order to actually get this video so go ahead will be a link in the description below if you haven't seen the pointers video definitely watch that okay so pointers and references are two kind of key words that are tossed around a lot in C++ and other languages they're essentially the same thing though I just want to start this video by saying that pointers and references are pretty much the same thing as far as what the computer will actually do with them now semantically so how we actually use them and how we write them there are some subtle differences but at the end of the day references are just pointers usually in disguise that's just all they are it's just intact sugar on top of pointers to make it a little bit easier to read and a little bit easier to follow a reference is essentially exactly what it sounds like it's a way for us to reference an existing variable unlike a pointer where you could create a new pointer variable and then set it equal to a null pointer or something like that that is equal to zero you can't do that with references because references has to reference an already existing variable references themselves are not new variable they don't really occupy memory they don't really have storage they're not like your typical variables because what they are instead is a reference to a variable I'm going to be bringing up a few different examples in this video however I'm going to keep it brief because the references aren't that complicated and the best way really to learn how to use them is actually to start using them so adversaries goes on we'll be using them all the time and you'll see what kind of the best practices or at least my opinion on what the best practice is to do with references however pohjanmaa doesn't code let's say that I make a variable I'll call it a and all political decide is just going to be an integer if I want to create a reference to that variable I can do so by typing type of the variable followed by an ampersand note here that the ampersand is actually part of the variable declaration in the previous video about pointers we learned that if we create a pointer we can use the ampersand operator here to actually take a memory address of an existing variable it's different here because the ampersand is actually part of the type if not you can see it's not actually next to an existing variable it's part of the type so just note that difference because with a lot of people as soon as you see an ampersand I think is the reference or oh it's definitely an address off it's kind of depending on context here so in this case because it's next to the type it's a reference so if we keep writing this I'll call this wrap and I'll set this equal to a and that's really all you have to do there's no kind of weird operator or something that we have to use here we just set an equal to an existing variable and so what we've done now is we've essentially created something called an alias because this rest variable I say variables in quotes here because it's not really a variable it's just a reference like this rest variable doesn't actually exist it just exists in our source code if you compile this code right now you're not going to get two variables created a and rest you're just going to have a what we can do now is we can use wrap as if it was a so if we set wrap now equal to two and then print a we're on our program here you're going to say that a is equal to two now because we're printing a and we've just changed crap to 2 because for all intents and purposes rest is a we've just created an alias for a and in this case our reference isn't a pointer or anything like that there's no need for the compiler to actually create a new variable if you compile this code it's just going to after this we just set a to 2 because that's really what we did this is just something that we can write in our source code to make our life easier if we wish to alias a variable now let's try something a little bit more complicated suppose that we wanted to write a function which was supposed to increment an integer if we just write the function this and then do something like value plus plus what will actually happen is if I create my integer over here and then I call increment with a parameter what's going to happen here is since we are just passing this by value you can see that we're not passing this other pointer or as a reference or anything like that it's actually going to copy this value five each of this function just copy it's going to create a brand new variable called value with that so it's almost as if we were to write something like that that's exactly what will happen so instead of that and I can prove it of course if I just roll my code here and we love this we're actually going to still end up with five printing to the cons well which can stay over here what I need to do is I actually need to pass this variable by reference in order for it to increment because what I really want to do is I want to actually affect this variable here so how can I achieve that how can I actually modify this variable by passing into a function now last time we talked about pointers and remember pointed on memory addresses so theoretically I hope you're putting this together in your mind because so exciting but theoretically what we could do really clever right is we could instead of talking the actual value five into that function which could pass the memory address of this a variable because then what we can do in that function is we could look up that memory address see that value five and then just modify that that memory address we can write through that memory address since we've taught that memory resident function look at that shot what although they'll modify this to actually take in a pointer and I'll just also call it value and then what I'll do here in increment is I'll pass the memory address of a instead of just the value five it's now passing the memory address of this variable one more thing I'll have to do is actually if you reference the value so that we can actually write through that memory instead of modifying the pointer itself if I just do this without the dereference it's going to just increment that address right so points are of course just memory address just an integer if I do value for cloth without the asterisk without the dereference operator then it is just going to increment my memory address not the actual value there now because of the order of operations it's actually going to do the increment first the reference so what I want to do instead is the reference first and then increment because I don't want to increment the pointer and then be reference that I want to dereference the pointer first and then increment the value at Point a so if I compile the code on it you will see that I will get do you think printing so awesome we've successfully managed to pop and variable by reference into a function however there's many of our references so we can do exactly what we just did here a lot easier with less code and with less kind of decorating our pin tax if we just use a reference and that's what I would do in this situation I would use a reference so what we can do is rewrite this to take in a reference instead of a pointer and then what that means is that I lose this whole need of dereferencing I can change this back to exactly what it was and then over here I don't need to pass the memory address today I can just pop in a and since it is being passed by reference we basically rewritten the code to do exactly the same thing internally when it gets compiled it will be exactly the same as what we've written before however this time it just looks nicer in our source code again that's the only difference so I run my program you can see we get this so that is all the references really are they are just syntax childer references there is nothing you can do with a reference that you cannot do with a pointer pointers are like references except they're even more useful they're even more powerful however if you can get away with using a reference like what we just did here absolutely use a reference because it's going to be a lot cleaner and simpler to read not the point is are really really difficult to read but it will look a lot cleaner in your source code one other important thing that I want to mention with references is that once you declare a reference you cannot change what it references what I mean by that is this suppose that I had two integers i had a and b and b were set to eight they were set to five and then what i wanted to do is declare a reference that was set to a and then maybe later on in my code i decided i actually you know what I want to set my reference should be referencing B can I do something like that the answer is no you can't do that what will happen in this case is that it will effectively create a reference to a and then when you decide to set that tickled to be it was just that a equal to the value of being which is a so what you will end up with here is a being equal to a and B being equal to eight that is that's it that's what you'll get and of course because of that that also means that when you declare a reference you need to actually assign into something you can't just do bit consider to be compiler will not let you do that it requires an initialize when you declare a reference you have to immediately assign it something because it has to reference something because remember it's not really a real variable at the reference so in this example specifically what would I do if I actually wanted this kind of functionality if I wanted to actually change what rest or the reference of well of course as I mentioned earlier this isn't a real variable we would need to create some kind of variable of sorts that we could set up 2.28 first and then later on we could switch it to point to be and of course I keep saying the word point in the hope you'll understand that I'm talking about a point so instead of this let's just make this a pointer we can initially set it to the memory address of a and then when I wanted to switch to it being pointing to B I can just switch it like this because remember to actually start assessing the value that this pointer is pointing to I need to be reference the pointer first and then state vehicle to something like that so in this case I'm setting a equal to two and in this case I'm setting B equal to 1 and if I were to log both of these they should be 2 and 1 and you can see this AR all right that's pretty much all I want to say really simple stuff will be using reference there's a lot more in the future again they're one of those things that you just use all the time like pointers so you'll see a lot more examples down the road as this variant progresses enjoy this video please hit the like button you can follow me on Twitter and Instagram and of course if you really enjoy this video you can support me on patreon to make sure that I can continue this if applause series videos and make more awesome videos in the future all right next time next time we're going to kick it up and off I'll see you guys later goodbye [Music] [Music]
Info
Channel: The Cherno
Views: 214,694
Rating: 4.9815531 out of 5
Keywords: thecherno, the, cherno, project, thechernoproject, c++, how c++ works, learn c++, c++ tutorial, game, programming, development, engine, game programming, game development, how to make a game, tutorial, source, code, complete, game engine, how to make a game engine, opengl, glfw, C (Programming Language)
Id: IzoFn3dfsPA
Channel Id: undefined
Length: 10min 12sec (612 seconds)
Published: Sun Jun 18 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.