WEBVTT

00:00:00.340 --> 00:00:03.400
We are almost done with the first module. I just want to mention how

00:00:03.400 --> 00:00:08.150
the keyword const works in relation to pointers and references. When we

00:00:08.150 --> 00:00:11.790
use the const keyword in our regular variable declaration, we are

00:00:11.790 --> 00:00:15.960
making a promise that this initial value will not be changed throughout

00:00:15.960 --> 00:00:17.160
the rest of the program.

00:00:17.940 --> 00:00:19.320
If you try to change it,

00:00:19.320 --> 00:00:24.060
compiler will remind you that this variable was not intended to be modified.

00:00:25.040 --> 00:00:30.150
Since references are just aliases for some other variable, the const keyword

00:00:30.150 --> 00:00:32.659
will basically affect them in the same way.

00:00:33.340 --> 00:00:35.900
If I put const in the reference declaration,

00:00:35.910 --> 00:00:38.990
I'm making a promise that I will not modify the value

00:00:38.990 --> 00:00:41.260
which this reference is referring to.

00:00:42.140 --> 00:00:46.440
However, since this variable also has access to the same place in memory,

00:00:46.450 --> 00:00:50.940
we could use it to change this value anyway. This is why we say

00:00:50.940 --> 00:00:53.080
that the const keyword is just a promise.

00:00:53.080 --> 00:00:56.250
There are still ways like this one to change the value at the place

00:00:56.250 --> 00:00:59.470
in memory which was meant to be constant, however,

00:00:59.480 --> 00:01:02.750
this is really not the common use case. Instead,

00:01:02.760 --> 00:01:05.410
let's say that we created a function which will print out the

00:01:05.410 --> 00:01:08.060
contents of our variable from the parameter.

00:01:09.040 --> 00:01:11.980
Since references are mostly used in this context,

00:01:11.990 --> 00:01:16.150
it makes more sense to combine them with the const keyword in this situation.

00:01:16.940 --> 00:01:20.300
Now you can pass arguments by reference, but you can be sure that the

00:01:20.300 --> 00:01:24.580
function will not be able to modify the contents of that argument, it will

00:01:24.580 --> 00:01:27.560
only be able to read the data from that place in memory.

00:01:29.040 --> 00:01:32.710
In other words, it is like you created a temporary second name for

00:01:32.710 --> 00:01:37.470
that argument which is constant only inside of this function and

00:01:37.470 --> 00:01:40.640
this makes sense because this function should not be making any

00:01:40.640 --> 00:01:42.560
changes to that data anyway.

00:01:43.440 --> 00:01:46.950
We'll talk more about constant references in the following modules.

00:01:48.140 --> 00:01:51.180
Const with pointers is a little bit more complicated

00:01:51.190 --> 00:01:54.000
because pointers are a little bit more complicated than

00:01:54.000 --> 00:01:56.280
references and regular variables.

00:01:56.290 --> 00:01:58.410
When you declare constant pointers,

00:01:58.420 --> 00:02:02.310
you should only pay attention to one thing is the const keyword

00:02:02.310 --> 00:02:06.160
placed before or after the asterisk character.

00:02:06.340 --> 00:02:09.419
If the const keyword is placed before the asterisk,

00:02:09.430 --> 00:02:12.060
that means that it is closer to the data type.

00:02:13.340 --> 00:02:16.650
It does not matter if you put it before or after it. As

00:02:16.650 --> 00:02:20.560
long as it is before the asterisk, it offers the same functionality.

00:02:21.640 --> 00:02:26.950
It means that you cannot change the data at which the pointer is pointing to.

00:02:26.960 --> 00:02:27.850
In other words,

00:02:27.860 --> 00:02:31.610
if you try to dereference this pointer and assign it another value,

00:02:31.620 --> 00:02:36.160
compiler will remind you that you made a promise and that this is not allowed.

00:02:36.840 --> 00:02:37.520
However,

00:02:37.530 --> 00:02:41.520
it is still completely legal to dereference this pointer for reading purposes,

00:02:41.530 --> 00:02:44.360
you are just not allowed to modify that data.

00:02:45.340 --> 00:02:49.870
If we put a const keyword after the asterisk closer to the pointer identifier,

00:02:49.870 --> 00:02:53.560
that means that we don't want it to point to anything else.

00:02:54.040 --> 00:02:59.150
In other words, the memory address that this pointer holds should not be changed.

00:02:59.160 --> 00:02:59.950
However,

00:02:59.960 --> 00:03:03.910
we can still dereference the pointer and modify the value from that address.

00:03:03.920 --> 00:03:07.660
We are just not allowed to point to some other place in memory.

00:03:07.810 --> 00:03:11.430
Of course, you can combine both of these approaches so that your

00:03:11.430 --> 00:03:14.950
pointer is secure from any modifications whatsoever.

00:03:15.940 --> 00:03:19.960
The use case for this will again be the passing of an argument by reference.

00:03:21.240 --> 00:03:23.690
If we put const before the asterisk,

00:03:23.700 --> 00:03:26.960
we are basically doing the same thing we did with references.

00:03:27.740 --> 00:03:30.820
We are making sure that this function cannot change the data

00:03:30.820 --> 00:03:32.750
that this pointer is pointing to.

00:03:33.540 --> 00:03:34.940
In most situations,

00:03:34.950 --> 00:03:38.710
it also makes sense to put the const after the asterisk so

00:03:38.710 --> 00:03:41.810
that we can be sure that we never lose the address held by

00:03:41.810 --> 00:03:44.760
the pointer. And that's it.

00:03:44.760 --> 00:03:47.360
We finally reached the end of this first module.

00:03:47.740 --> 00:03:50.690
It is essential that you understand these concepts so that we

00:03:50.690 --> 00:03:53.280
can build upon them in the following modules.

00:03:53.330 --> 00:03:55.520
If you have any questions whatsoever,

00:03:55.530 --> 00:03:58.420
don't hesitate to ask them in the courses discussion

00:03:58.650 --> 00:04:00.600
and please don't skip the summary.

00:04:00.610 --> 00:04:04.260
It can help you solidify the knowledge before moving on with the course.
