WEBVTT

00:00.490 --> 00:08.060
It's oh last time we talked about pointers and in this lecture I want to talk about a concept that's

00:08.060 --> 00:11.000
very similar which is references.

00:11.270 --> 00:18.420
So a reference it is a variable that also has an address as its value.

00:18.860 --> 00:23.670
So there are some key differences between pointer and a reference.

00:23.690 --> 00:26.870
But first let's look at how to make one.

00:27.140 --> 00:44.020
So we had X was 5 in a reference and that uses the ampersand instead of the star we have four in set

00:44.170 --> 00:46.270
to X like this.

00:47.010 --> 00:54.870
OK so don't get this ampersand can use with address when it's in a declaration for a variable this means

00:54.870 --> 00:55.770
reference

00:58.100 --> 00:59.140
so.

00:59.190 --> 01:07.880
Notice also that we set the reference and we're not taking the address of X.

01:08.190 --> 01:15.200
OK so before we had we had the address of X for pointers but we don't need to do that it possible sort

01:15.200 --> 01:18.090
of do that for us with references.

01:18.430 --> 01:21.910
K so there is also something else to note.

01:22.100 --> 01:25.290
And that's not really immediately obvious.

01:25.550 --> 01:29.370
And that is you can't use something like this.

01:29.420 --> 01:34.300
So we had another reference in reference to.

01:34.600 --> 01:39.460
You can't do this in you'll see if you try to do this.

01:39.760 --> 01:48.380
OK so says the declaration a reference variable and ref 2 requires initializer.

01:48.520 --> 01:56.050
And so it needs to have an initial value at the start k and that is because a reference is really an

01:56.080 --> 01:57.410
alias.

01:57.650 --> 02:02.560
It needs something to reference as it were right.

02:02.920 --> 02:05.690
Otherwise it it doesn't make any sense.

02:06.190 --> 02:13.600
OK so you can't have a reference that doesn't reference anything that doesn't it doesn't work.

02:13.600 --> 02:19.690
So this is actually pretty similar to constants if you're a member or so constant in.

02:20.080 --> 02:25.120
You know Max you couldn't have a concept like this for example

02:28.220 --> 02:31.340
in this.

02:31.380 --> 02:31.810
Right.

02:31.920 --> 02:37.130
So default initialization of object of conc type constant.

02:37.140 --> 02:38.150
Right.

02:38.310 --> 02:41.230
So this is very similar.

02:41.420 --> 02:49.440
So and this should make sense because you can't change Max later because it's a constant.

02:49.440 --> 02:51.900
So it's the same thing for references.

02:55.550 --> 03:06.000
So another key difference is between between the references and pointers is that you don't need to dereference

03:06.000 --> 03:12.960
a reference in order to change the value pointed to by the reference.

03:12.970 --> 03:25.330
So we had this if you want to change it to say giraffe to 10.

03:25.690 --> 03:33.020
And if you want to see out the value x is

03:35.060 --> 03:37.860
x and it's

03:40.640 --> 03:41.930
X is 10 right.

03:41.960 --> 03:45.010
So the reference actually changed x.

03:45.200 --> 03:50.380
So we noticed you didn't use the dereference operator star operator.

03:50.380 --> 03:52.840
You remember from last time.

03:52.910 --> 03:57.890
That's because again C++ sort of does that for you already.

03:57.890 --> 04:02.220
So there's no need to use it in order to change the value.

04:04.550 --> 04:11.930
So I really think of references as the original variable self.

04:12.050 --> 04:15.510
So that's really my mental model for them.

04:15.590 --> 04:19.860
I can do it any operation that I can do with the original.

04:19.920 --> 04:30.740
So might be asking then when should I use a reference over a pointer and just use a pointer when you

04:30.740 --> 04:33.890
want it to point to either nothing.

04:33.890 --> 04:42.620
I remember that null pointer or a real value and you should use reference when you don't always want

04:42.620 --> 04:45.160
to point to a real value.

04:45.550 --> 04:50.690
OK so there's this subtle but it's it's very important.

04:50.690 --> 04:54.260
So this will become much more clear as we start using them.
