References in C++ Explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome everybody it's caleb this is going to be a pretty exciting video and the upcoming videos because we're going to be talking about references and pointers inside of c plus this is actually a point of confusion for a lot of beginners but i'm going to make it simple for you guys put simply references allow us to refer to different areas of memory and we're going to get into the details of what that means as well as why you would want to use a reference and how to actually make one but first we got to talk about our sponsor did you play the clip c plus plus builder is the ide of choice for rapidly building c plus plus applications utilize drag and drop visual components that are responsive and allow for cross-platform deployments when building data-driven applications you can bind data sources to visual components to make working with data easy go ahead and get started with a free trial of the architect edition which will give you all of the features of c plus builder so whether you're a beginner just getting started or want to build enterprise level applications c plus builder is the tool for you i'll leave a link in the description so the first thing i want to get on the table or should i say chalkboard is what is a reference all right so like i said i can't talk and type at the same time typing this is called typing a reference allows us to refer to some area of memory and you're going to see this if for example you create a variable we'll just call it a and it's going to contain the value 5. so the way you would do this in c plus it's really hard int a is five so we just created that variable we can create a reference to the same location and it's going to look like this or something like that and the way you would create it is by saying int and then using the ampersand capital 7 i think give it some other name such as b and assign it the value a that's it that is how you create a reference and now you can refer to this value here in two ways the first way with a and the second way with b so you can think of a reference as an alias it's another name for a variable so the very first reason you might want to use a reference is if you need an alias for some data you want to refer it by a and b so if you have to do that for some reason then you might want to look into references now c plus plus can be a little bit confusing because not only do you have references but you also have pointers and behind the scenes references actually use pointers however we don't need to understand how pointers work or how to use them in order to use references in fact most of the time you don't need to use pointers inside a c plus plus and references are actually the preferred thing to use so i would recommend you first understand references before you study pointers now i mentioned you can use references if you need multiple identifiers to refer to the same data in memory however this is not really the most common use of references you will most see them when we're working with functions and passing data to these functions so with that it allows us to refer to the same exact area of memory inside and outside of the function and i'm going to explain exactly what that means here soon so know that that's coming but before we talk about functions i first wanted to talk a little bit about how to actually work with these references because it's very simple which is why we need to talk about it when you create a reference there's a special syntax so like in the example we just had we had some variable a we assigned it a value and then we had the reference which had the ampersand and that can go attached to the int so no space between the int and the ampersand or there can be a space there or it can be attached to the actual variable name here so you're going to see all three variations and then you just assign it another variable here so we assign it a and now these both point to the same area of memory so just to repeat what i said here you might see it as int ampersand space b int space ampersand space b and int space ampersand b all three of these are valid so don't worry about which one to use you're going to see all of them probably in your c plus development so the important thing to realize when you're working with references is that there is some syntax you have to know when you're creating the reference just the ampersand but when you actually use the reference you don't have to do anything special at all so you can treat a and b as the same exact thing what that means is if you wanted to do some output you could output a and then you could follow it up with an m line you could output b and since these are both integers you could use them inside of expressions so you could say a plus 5 or you could say b plus 5. so basically what i'm trying to show you is that you don't have to use the reference in a special way you use it just like a regular integer that's because it refers to an integer value specifically this 5 right here so yeah to summarize just make sure that you understand the syntax part comes when you create the reference but you don't have to worry about anything different when you actually use the reference once you do this you're good one of the downsides here is that when you're working with a variable and you just see b you can't immediately know if that is a reference or if it was the original variable it really shouldn't matter because you can use them either way however it's just important to know that this is different than pointers where with pointers you actually have to use them a different way so we'll get into that later on so now i want to talk about the actual most common use case of references and that is for passing data to functions so let's say we have some function and we'll just call this function work and it has a parameter we're just going to call it x and we're not going to do anything inside of this function right now now what we're going to do is we're going to invoke this function so we can say int a has the value 5 and then we can call work and pass in our variable a so what happens here is this value notice we're not doing anything with references yet this is just standard passing by value so what happens here is when we pass in a the value 5 gets copied to this variable x so in memory it's going to look something like this we have the variable a with the value 5 and then we have the variable x which gets the value 5 when we pass it in for simple types like integers this is perfectly fine and actually for small objects this is going to work fine as well the thing you need to understand though is that if you are working with extremely large amounts of data so for example if you just have a vector with tons and tons of data like legally a crap ton of data then you can pass it by reference and that data is not going to be copied so let's just look at it with this situation here where we're working with integers if we created this parameter as a reference by putting an ampersand right there now this is going to refer to the same data so this has the immediate benefit of saving memory for large data i add because if you're just working with integers it's not going to make a difference but imagine the data you're working with is very large you're only going to have one one copy of it in memory and then you're just going to refer to it from the parameter so that is the immediate benefit you're going to see the immediate consequence and it could be a pro or a con is that you can change the data within the function so usually this is a good thing if you're creating a function and you want to be able to modify the parameter variable that was passed in you can use references so in this situation we could do this x plus plus that's going to change this data right here to a six and notice that it changed it where a is so that means down here in our code a is going to have the value 6. so those changes persist beyond the function so it's obviously good if you want to change the data inside of a function however it can bring up an issue where you're not expecting a function to change your data you pass it in and it changes the data on the outside so you just have to be aware that whenever you use references that data can be changed whether that's good or bad usually it's a good thing though so i want to go over a simple example where this might be a bad thing let's say we are working with a function to print out user information for our application into the console so the function might look like this void and we can just call it print and this takes a user object but it's a reference here inside of here we expect it to do some outputs but little do we know it's actually modifying the data so it might do something like u dot name and assign it a new name and that's bad because from the calling side you wouldn't expect this at all based on the name of the function so you would invoke this like print and you would pass in some user fully expecting user to be exactly the same down here but it's not you just got trolled so now i want to go over an example of where this is a good thing and that would be swapping data so if you created a function we can just call it void swap and this can take two things let's just say integers and we're getting them by reference so into x and int y and then you can do the standard swap algorithm so you create a temporary variable assign an x and then you assign y to x and then you assign temp to y i think that's right and this is going to work inside of c plus plus and this can be used to build different things such as different sorting algorithms if we didn't have this by references we wouldn't be able to modify the variables passed in and it would be unaffected but fortunately because we have references this is going to work so if we have int x being 0 int y being 10 we invoke swap and pass in these two variables after swap right here we're going to have x being 10 and y being 0. cool so that's the basics of working with references now i want to give you a few extra tips maybe a couple of gotchas that you just need to watch out for so let's talk about that now if you have a variable a and we will assign it the value 10 and then you create a reference to this variable and we will call it b again this is just a review real quick a is going to contain the value 10 and we'll draw that here so that is a representation of what it might look like in memory and then b is going to refer to that the thing i want to call out here is that you cannot change where b refers to so it's not possible to make this refer to somewhere else in memory this can't be done so the location we set to the reference is permanent a point of confusion that might come up is what if you do reassign this reference let's say we have another variable so we'll say int c is assigned the value 100 and then we take b and assign it the value c syntactically it appears as though we might be changing the reference b to now point to c but that's actually not what's happening because if you remember anytime we use b it's talking about this location of memory so what's actually happening is it's changing this location of memory to whatever value c is so it might look something like this c is 100 when we take c and assign it to b b refers here so the value 100 is copied over to a changing it from 10 to 100 so that's a little bit confusing and maybe that's a bit more depth than you need for this introductory video however it's just important to know that you can't reassign the location that a reference refers to whenever you're not positive what's going on and you just need a little extra convincing you can use an operator known as the address of operator which is also the ampersand but it's not the same exact thing as when we're creating a reference so anytime you put this before a variable such as the variable a it's going to give you the address of this variable so you can just pass this to c out and it will tell you that location so you can do this to basically prove that a and b refer to the same area in memory this will probably come on more when we talk about pointers but just for now it's a good way to just test to make sure things are working the way you expect thank you everyone and please be sure to hit subscribe peace out [Music] [Music] you
Info
Channel: Caleb Curry
Views: 28,257
Rating: 4.9750781 out of 5
Keywords: references in c++ explained, c++, references, explained, code, coding, caleb, curry, programming, computer science, pass by value, pass by reference
Id: OCL7mSFCIx0
Channel Id: undefined
Length: 15min 3sec (903 seconds)
Published: Thu Oct 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.