﻿1
00:00:04,340 --> 00:00:05,570
‫Hello and welcome.

2
00:00:05,570 --> 00:00:12,050
‫In this lecture we are going to be learning about references and alternative to pointers, and we're

3
00:00:12,050 --> 00:00:14,810
‫going to see why they're different, why we might want to use them.

4
00:00:14,810 --> 00:00:16,370
‫Let's dive in and see.

5
00:00:17,220 --> 00:00:23,200
‫So as well as pointers, there's a very similar related concept called a reference.

6
00:00:23,220 --> 00:00:28,980
‫Now both pointers and references are essentially just a memory address.

7
00:00:28,980 --> 00:00:36,510
‫They both store a reference to where another variable can be found, and in this way they are incredibly

8
00:00:36,510 --> 00:00:37,200
‫similar.

9
00:00:37,230 --> 00:00:43,140
‫The difference between the two comes to basically down to whether they can be reassigned.

10
00:00:43,140 --> 00:00:48,650
‫Basically, can they point to a different address after they've first been created?

11
00:00:48,660 --> 00:00:51,570
‫In the case of a pointer, that answer is yes.

12
00:00:51,570 --> 00:00:57,660
‫You can change a pointer to point to a new memory address, a new actor, a new component whenever you

13
00:00:57,660 --> 00:00:59,910
‫like, as long as it has the right type.

14
00:01:00,210 --> 00:01:06,390
‫A reference, on the other hand, can only be set up once when you first create it to point to another

15
00:01:06,390 --> 00:01:09,450
‫variable, it can't then be changed again.

16
00:01:09,450 --> 00:01:16,260
‫And this is a safety feature because repointing variables can lead to problems where those variables

17
00:01:16,260 --> 00:01:21,480
‫might disappear or where you set that pointer to null.

18
00:01:21,510 --> 00:01:28,020
‫A null pointer is essentially a pointer that points to nothing, and trying to do stuff with a pointer

19
00:01:28,020 --> 00:01:35,040
‫that sets to nothing can actually crash your engine, which isn't an ideal result with references because

20
00:01:35,040 --> 00:01:37,410
‫you have to give them a value when you first create them.

21
00:01:37,560 --> 00:01:41,730
‫They can't be null, so you cannot get these null pointer crashes.

22
00:01:41,940 --> 00:01:45,660
‫So let's start to have a look at some of the syntax involved.

23
00:01:45,660 --> 00:01:50,880
‫So when you access content with a pointer, you have to use some special syntax.

24
00:01:50,880 --> 00:01:55,890
‫You have to put this star in front of the pointer to make sure you're getting the value in the pointer,

25
00:01:55,890 --> 00:02:01,200
‫not the memory address with references because you never really need to get hold of the memory address.

26
00:02:01,200 --> 00:02:06,240
‫You can just use the variable as if it was the variable it's pointing to.

27
00:02:06,240 --> 00:02:08,460
‫You don't have to use any special syntax.

28
00:02:08,820 --> 00:02:15,390
‫Now there's accessing the address, so with pointers you get hold of the address automatically.

29
00:02:15,390 --> 00:02:18,750
‫When you use the variable with a reference, you have to use special syntax.

30
00:02:18,750 --> 00:02:24,480
‫To do that, you have to use the address of operator and it will get you basically the same address

31
00:02:24,480 --> 00:02:26,670
‫as the original variable.

32
00:02:27,210 --> 00:02:30,720
‫And then there's changing the address with a pointer.

33
00:02:30,720 --> 00:02:35,850
‫We've seen that you can assign the address and use the address of operator to do that with references.

34
00:02:35,850 --> 00:02:38,070
‫This isn't allowed, so there's no syntax for it.

35
00:02:38,070 --> 00:02:43,860
‫And finally, what if we want to change the value that we're pointing to, not the address, but the

36
00:02:43,860 --> 00:02:46,200
‫actual value being stored at that address?

37
00:02:46,200 --> 00:02:50,740
‫Well, with the pointers you got to again use some special syntax.

38
00:02:50,740 --> 00:02:53,280
‫We have to put that star and then assign something.

39
00:02:53,280 --> 00:03:00,750
‫So the actor pointer you assign to the star actor pointer with references again because you can't change

40
00:03:00,750 --> 00:03:03,270
‫the actual address, you don't need any extra syntax.

41
00:03:03,270 --> 00:03:07,230
‫We can drop the extra syntax and just have the actor reference equals.

42
00:03:07,230 --> 00:03:12,870
‫Actor means that you are overwriting the value in the pointed to location.

43
00:03:12,870 --> 00:03:14,280
‫So that's all a bit theoretical.

44
00:03:14,280 --> 00:03:16,200
‫Let's have an actual example.

45
00:03:16,200 --> 00:03:24,540
‫So if we had a float that was perhaps storing the damage done to us in the game and in that we are storing

46
00:03:24,540 --> 00:03:26,670
‫the current damage is set to zero.

47
00:03:26,670 --> 00:03:28,350
‫No one's done any damage to us.

48
00:03:28,380 --> 00:03:33,900
‫Now we might want to create a reference to this to instead of copying around, we might want to pass

49
00:03:33,900 --> 00:03:36,960
‫it into a function, but without actually changing it.

50
00:03:36,960 --> 00:03:42,690
‫The way we can do this is to have a float and then put the ampersand after the float.

51
00:03:42,690 --> 00:03:45,390
‫And the ampersand says that this is a reference to a float.

52
00:03:45,390 --> 00:03:49,470
‫It is not a new float itself, doesn't allocate any float memory.

53
00:03:49,470 --> 00:03:51,360
‫It just allocates a pointer memory.

54
00:03:51,510 --> 00:03:56,940
‫And so we can call this the damage ref and we can set it equal to damage.

55
00:03:56,940 --> 00:04:00,360
‫We don't have to take the address of or do anything like that.

56
00:04:00,360 --> 00:04:04,530
‫It automatically knows, well, this is a reference and this is a variable.

57
00:04:04,530 --> 00:04:08,760
‫So what you're trying to do here is set up a reference to that variable.

58
00:04:08,760 --> 00:04:15,870
‫And sure enough, if we go and you log, then we can print this as if it were just a regular float so

59
00:04:15,870 --> 00:04:24,540
‫we can say damage colon and then percent F and then here we can do just print damage ref.

60
00:04:24,540 --> 00:04:25,860
‫We don't need any special syntax.

61
00:04:25,860 --> 00:04:28,470
‫We can use it just like a regular variable.

62
00:04:28,470 --> 00:04:34,020
‫Let's go ahead and hit play after compiling, have a look at our output log and sure enough, the damage

63
00:04:34,020 --> 00:04:37,680
‫is coming out at zero, which was the value that we set.

64
00:04:37,680 --> 00:04:42,720
‫But that doesn't necessarily prove that I haven't made a copy of that original variable.

65
00:04:42,900 --> 00:04:47,040
‫So let's say challenge you to try changing the value and see what happens.

66
00:04:47,040 --> 00:04:54,720
‫So use the reference, the reference to damage to set a new value and then print out the original value

67
00:04:54,720 --> 00:04:59,910
‫to so the original value next to the reference value and have a look.

68
00:04:59,910 --> 00:05:01,770
‫Are they the same or are they different?

69
00:05:01,770 --> 00:05:05,190
‫You only change the reference, but does it change the original as well?

70
00:05:05,190 --> 00:05:08,760
‫Because that would imply that they are both the same variable.

71
00:05:08,790 --> 00:05:10,830
‫Pause the video and have a go.

72
00:05:13,910 --> 00:05:14,420
‫Okay.

73
00:05:14,420 --> 00:05:15,130
‫Welcome back.

74
00:05:15,140 --> 00:05:16,610
‫So let's give this a stab.

75
00:05:16,640 --> 00:05:22,310
‫So to change the reference now, we're going to do damage ref equals.

76
00:05:22,310 --> 00:05:27,320
‫And this time I'm going to say that we did five damage and notice I haven't had to use any special syntax

77
00:05:27,320 --> 00:05:27,650
‫here.

78
00:05:27,650 --> 00:05:33,140
‫It's not repointing the reference because we've already declared it and set its reference up on that

79
00:05:33,140 --> 00:05:34,760
‫first line when it's created.

80
00:05:34,760 --> 00:05:36,290
‫We can't do it again.

81
00:05:36,290 --> 00:05:39,080
‫So now we can just be setting the value.

82
00:05:39,410 --> 00:05:46,010
‫So what I can do now is we've got the damage, ref is in the log.

83
00:05:46,040 --> 00:05:56,810
‫Let's also put a damage and percent f onto that same string and then pass the damage in here as well.

84
00:05:57,560 --> 00:06:00,710
‫And then we're going to compile and have a look.

85
00:06:00,740 --> 00:06:08,540
‫Let's hit play, go into our output log and sure enough, it's printing out damage ref and damage as

86
00:06:08,540 --> 00:06:12,350
‫both having the new value that we have set the value of five.

87
00:06:12,350 --> 00:06:18,710
‫So in essence, what we've got here is two variables that point to the same piece of memory.

88
00:06:18,710 --> 00:06:22,370
‫You could almost say that they are the same variable, but with different names.

89
00:06:22,370 --> 00:06:28,610
‫So this has the benefit of not needing a copy, just like a pointer, but it also has a lot more benefits

90
00:06:28,610 --> 00:06:30,740
‫because it's so much easier to use.

91
00:06:30,740 --> 00:06:33,680
‫We don't need all that extra syntax and it's also safer to use.

92
00:06:33,680 --> 00:06:39,320
‫So as long as we don't need to re point anything, using references is generally preferable.

93
00:06:39,320 --> 00:06:44,840
‫Now let's do a quick review of some of the symbols we use here because they can get a little bit confusing.

94
00:06:44,840 --> 00:06:47,240
‫They're used a lot in C++.

95
00:06:47,240 --> 00:06:52,520
‫So the ampersand and the star symbols, let's have a look at them in context and what they mean.

96
00:06:52,790 --> 00:07:00,020
‫So when we are using them, so when we are using them in an expression, here are some code examples.

97
00:07:00,020 --> 00:07:03,620
‫You might see the star before a variable.

98
00:07:03,620 --> 00:07:06,410
‫You might see the ampersand before a variable.

99
00:07:06,410 --> 00:07:07,610
‫What does this mean?

100
00:07:08,240 --> 00:07:16,190
‫Well, with the star symbol, the syntax of putting it before the variable means we're getting the contents

101
00:07:16,190 --> 00:07:20,990
‫at actor pointer now with the ampersand.

102
00:07:20,990 --> 00:07:25,940
‫If you put the ampersand in front of a variable or in front of a reference to a variable, it's saying

103
00:07:25,940 --> 00:07:28,910
‫that it's getting the address of that variable.

104
00:07:28,910 --> 00:07:32,720
‫It's getting the address of the actor or the address pointed to by the reference.

105
00:07:32,720 --> 00:07:39,440
‫Now when we are declaring we were using it in the type of a variable, the stars and the ampersand have

106
00:07:39,440 --> 00:07:41,420
‫almost the opposite meanings.

107
00:07:41,420 --> 00:07:51,110
‫So the star means that this is a pointer to a new actor, and the ampersand means this is a reference

108
00:07:51,110 --> 00:07:52,550
‫to a new actor.

109
00:07:52,550 --> 00:07:57,140
‫So just remember this, that there is a completely different set of meanings, whether you're using

110
00:07:57,140 --> 00:08:03,500
‫them with a variable in an expression or whether you're using them in the type declaring a variable.

111
00:08:03,500 --> 00:08:08,000
‫Now, I won't go into it in too much detail, but to add insult to injury, these can actually have

112
00:08:08,000 --> 00:08:14,240
‫other meanings in other contexts that ampersand can mean a bit wise, and the star, as we've already

113
00:08:14,240 --> 00:08:17,960
‫seen, can be used with strings in the U log.

114
00:08:17,960 --> 00:08:22,610
‫So this isn't necessarily all of the meanings of these two symbols, but they are pretty much all the

115
00:08:22,610 --> 00:08:26,600
‫important meanings referring to pointers and references.

116
00:08:26,630 --> 00:08:27,320
‫Great stuff.

117
00:08:27,320 --> 00:08:32,480
‫So we've learnt about pointers, references, how they relate to each other, how they are different,

118
00:08:32,480 --> 00:08:34,490
‫and why we might want to use them.

119
00:08:34,490 --> 00:08:36,140
‫I'll see you in the next lecture.

