‫Hello and welcome.
‫In this lecture we are going to be learning about references and alternative to pointers, and we're
‫going to see why they're different, why we might want to use them.
‫Let's dive in and see.
‫So as well as pointers, there's a very similar related concept called a reference.
‫Now both pointers and references are essentially just a memory address.
‫They both store a reference to where another variable can be found, and in this way they are incredibly
‫similar.
‫The difference between the two comes to basically down to whether they can be reassigned.
‫Basically, can they point to a different address after they've first been created?
‫In the case of a pointer, that answer is yes.
‫You can change a pointer to point to a new memory address, a new actor, a new component whenever you
‫like, as long as it has the right type.
‫A reference, on the other hand, can only be set up once when you first create it to point to another
‫variable, it can't then be changed again.
‫And this is a safety feature because repointing variables can lead to problems where those variables
‫might disappear or where you set that pointer to null.
‫A null pointer is essentially a pointer that points to nothing, and trying to do stuff with a pointer
‫that sets to nothing can actually crash your engine, which isn't an ideal result with references because
‫you have to give them a value when you first create them.
‫They can't be null, so you cannot get these null pointer crashes.
‫So let's start to have a look at some of the syntax involved.
‫So when you access content with a pointer, you have to use some special syntax.
‫You have to put this star in front of the pointer to make sure you're getting the value in the pointer,
‫not the memory address with references because you never really need to get hold of the memory address.
‫You can just use the variable as if it was the variable it's pointing to.
‫You don't have to use any special syntax.
‫Now there's accessing the address, so with pointers you get hold of the address automatically.
‫When you use the variable with a reference, you have to use special syntax.
‫To do that, you have to use the address of operator and it will get you basically the same address
‫as the original variable.
‫And then there's changing the address with a pointer.
‫We've seen that you can assign the address and use the address of operator to do that with references.
‫This isn't allowed, so there's no syntax for it.
‫And finally, what if we want to change the value that we're pointing to, not the address, but the
‫actual value being stored at that address?
‫Well, with the pointers you got to again use some special syntax.
‫We have to put that star and then assign something.
‫So the actor pointer you assign to the star actor pointer with references again because you can't change
‫the actual address, you don't need any extra syntax.
‫We can drop the extra syntax and just have the actor reference equals.
‫Actor means that you are overwriting the value in the pointed to location.
‫So that's all a bit theoretical.
‫Let's have an actual example.
‫So if we had a float that was perhaps storing the damage done to us in the game and in that we are storing
‫the current damage is set to zero.
‫No one's done any damage to us.
‫Now we might want to create a reference to this to instead of copying around, we might want to pass
‫it into a function, but without actually changing it.
‫The way we can do this is to have a float and then put the ampersand after the float.
‫And the ampersand says that this is a reference to a float.
‫It is not a new float itself, doesn't allocate any float memory.
‫It just allocates a pointer memory.
‫And so we can call this the damage ref and we can set it equal to damage.
‫We don't have to take the address of or do anything like that.
‫It automatically knows, well, this is a reference and this is a variable.
‫So what you're trying to do here is set up a reference to that variable.
‫And sure enough, if we go and you log, then we can print this as if it were just a regular float so
‫we can say damage colon and then percent F and then here we can do just print damage ref.
‫We don't need any special syntax.
‫We can use it just like a regular variable.
‫Let's go ahead and hit play after compiling, have a look at our output log and sure enough, the damage
‫is coming out at zero, which was the value that we set.
‫But that doesn't necessarily prove that I haven't made a copy of that original variable.
‫So let's say challenge you to try changing the value and see what happens.
‫So use the reference, the reference to damage to set a new value and then print out the original value
‫to so the original value next to the reference value and have a look.
‫Are they the same or are they different?
‫You only change the reference, but does it change the original as well?
‫Because that would imply that they are both the same variable.
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫So let's give this a stab.
‫So to change the reference now, we're going to do damage ref equals.
‫And this time I'm going to say that we did five damage and notice I haven't had to use any special syntax
‫here.
‫It's not repointing the reference because we've already declared it and set its reference up on that
‫first line when it's created.
‫We can't do it again.
‫So now we can just be setting the value.
‫So what I can do now is we've got the damage, ref is in the log.
‫Let's also put a damage and percent f onto that same string and then pass the damage in here as well.
‫And then we're going to compile and have a look.
‫Let's hit play, go into our output log and sure enough, it's printing out damage ref and damage as
‫both having the new value that we have set the value of five.
‫So in essence, what we've got here is two variables that point to the same piece of memory.
‫You could almost say that they are the same variable, but with different names.
‫So this has the benefit of not needing a copy, just like a pointer, but it also has a lot more benefits
‫because it's so much easier to use.
‫We don't need all that extra syntax and it's also safer to use.
‫So as long as we don't need to re point anything, using references is generally preferable.
‫Now let's do a quick review of some of the symbols we use here because they can get a little bit confusing.
‫They're used a lot in C++.
‫So the ampersand and the star symbols, let's have a look at them in context and what they mean.
‫So when we are using them, so when we are using them in an expression, here are some code examples.
‫You might see the star before a variable.
‫You might see the ampersand before a variable.
‫What does this mean?
‫Well, with the star symbol, the syntax of putting it before the variable means we're getting the contents
‫at actor pointer now with the ampersand.
‫If you put the ampersand in front of a variable or in front of a reference to a variable, it's saying
‫that it's getting the address of that variable.
‫It's getting the address of the actor or the address pointed to by the reference.
‫Now when we are declaring we were using it in the type of a variable, the stars and the ampersand have
‫almost the opposite meanings.
‫So the star means that this is a pointer to a new actor, and the ampersand means this is a reference
‫to a new actor.
‫So just remember this, that there is a completely different set of meanings, whether you're using
‫them with a variable in an expression or whether you're using them in the type declaring a variable.
‫Now, I won't go into it in too much detail, but to add insult to injury, these can actually have
‫other meanings in other contexts that ampersand can mean a bit wise, and the star, as we've already
‫seen, can be used with strings in the U log.
‫So this isn't necessarily all of the meanings of these two symbols, but they are pretty much all the
‫important meanings referring to pointers and references.
‫Great stuff.
‫So we've learnt about pointers, references, how they relate to each other, how they are different,
‫and why we might want to use them.
‫I'll see you in the next lecture.