- [Instructor] The C plus plus reference type is a lot
like a pointer but with some significant differences.
If you haven't viewed the lesson on pointers,
please take the time to do that now.
This lesson will make a lot more sense
if you understand pointers first.
So this is a copy of pointers.cpp
from a lesson on pointers.
And I'm just going to make couple changes here.
First we don't need these added lines,
and I'm going to take out Y, and instead
am going to create a reference called Y,
and I'm going to assign it to X.
So this creates a reference Y and it's a reference to X.
So you can use Y just like any other end,
but it's value is always the value of X.
You notice that when I use it down here in this print F,
I don't have to dereference it.
I don't have to put an astros before it
or anything like that.
And when I assign it, I assign it to the variable,
but I don't have to take it's address.
So it's a reference, it's not a pointer.
So when I build and run this obviously all of the values
are going to be seven because our pointer points to X
or our reference is a reference of X.
And if I changed the value of X,
you'll notice that all of those values
changed just like you expect.
But here's the thing that's a little bit different.
When I come down here on and make copy of all of these,
and right here, I'm just going to say, Y equal 73.
And when I build and run this,
you'll notice that Now, X is also 73.
Y is 73 and what's pointed to by the pointer is 73.
So, just by changing Y, I've changed X
and I've changed everything.
So, there's a couple of major differences between a pointer
and a reference that's worth understanding at this point.
First, you don't need to, dereference a reference.
So you don't use an asterisk,
no special punctuations required.
The value of the reference is always
the value of the referred variable.
Second, there's no syntax for changing and reference.
Once the reference is defined, it can't be changed.
When I change Y here, I'm not changing Y I'm changing X.
Y remains a reference of X.
So a reference is not even really a variable.
It's really just a trick of syntax.
It's an entry in the namespace without any value of its own.
Appointor is a variable of type pointer.
I can assign it to point to something else.
This is called receding the pointer.
So I can come down here and I can take copy of all of these.
And I can define a new variable into Z
and give it a value of seven.
And I can now say IP equals the address of Z.
And now when I print this out, when I run this,
you'll see that the value of what's pointed on IP
is now seven, which is the value of the new variable Z.
But a reference is not a variable.
It cannot be receipted to refer to a different value.
Once it's been declared, it cannot be changed.
It will always refer to the same variable.
So it's important to realize the danger of assigning
a value through a reference.
If I change a value through a pointer,
if I come down here and I say,
what's pointed out by IP equals 103,
now I can print out the value of Z,
and Z is now 103.
But if I change the value of Y,
I'll come down here and I'll make copies
of both of those again, and I change Y
to 142, you notice that it changes both X and Y to 142.
So, the problem is that it's not at all obvious
when I change X that I'm also changing Y.
You look at this line of code,
You don't see anything about X.
You only see Y, so I change Y and it also changes X
as a side effect, and it's not obvious at all.
So for this reason, I strongly recommend
that you always declare a reference
with the const to modifier.
So if I come back up here and I say const,
now I can no longer change X through Y.
See, it's not going to allow me to assign to this.
I can assign to X, and that's fine.
And the same down here, this line is no longer allowed,
but assigning the X is just fine because it's not const.
I've only const qualified the reference.
So I strongly recommend that you always
const modify your references.
And this is just going to save you a whole lot of headache,
so you're not actually affecting other things
through side effects.
References are used a lot in C plus plus,
and you'll see a lot of examples of them later.
And you'll come to understand why they're valuable
as we go through this course.
They are used especially in functions and classes.
You'll see a lot of examples.
C does not have references.
This is exclusively a feature of C plus plus.