As I mentioned, there is an easier way to pass objects by reference in C++.
Let's remove all of the asterisk characters to turn this pointer back into a
simple integer and also to stop dereferencing this variable.
I will also remove the & character because I'm no
longer interested in the address of x.
This is now the same program we had at the beginning of the previous lesson.
And if you remember, this program will pass the argument by value.
To pass it by reference in C++, there is only one simple thing you need to do.
Just add the & character before the name of the parameter.
Okay, let's try to compile the program again, and it worked.
As you can see, the last number is still 8,
which means that the value from the main function's variable,
x, was also changed.
And as you know, that means that we passed the reference of an argument.
But how does this work, and what did we actually do here?
Let's go step by step.
C++ allows us to use the so‑called references.
You can think of references in two ways.
The first way is to imagine that they are just pointers,
which are immediately dereferenced,
so you don't have to use the dereference operator.
But I think that this is just making the whole matter more confusing,
so let me create a reference, and we can see what is actually going on in memory.
I declared a simple integer variable and initialized it to the number 5.
On the next line, I'm creating a reference, y.
The reason you know that this is a reference and not a regular variable is
because of this & character in front of the identifier.
Just like we created pointers by adding the asterisk,
we create references by adding the & character.
The best way to think about references is to consider
them as an alias for another variable.
In other words, our reference is just another name for the same place in memory.
In this case, we are saying create the reference,
y, and assign it to the place in memory held by the variable,
x.
To demonstrate this,
I will print out the value from both variables and
then increment each one of them, and then I will again print out the values.
Let's compile this.
Both of the variables give us the number 5 at first,
and then after incrementing each one of them,
both of them have the number 7, and this makes sense.
As you can see, you can use a reference just like any other variable.
You just have to remember that a reference doesn't have its own place in memory.
It is used as a second name for another variable.
So when you create a reference,
you need to assign it to another variable immediately.
A reference cannot be null like a pointer.
Don't confuse references with pointers.
You can reassign a pointer to another address,
but you cannot reassign a reference because a reference already dereferenced.
It will always point to the same place in memory.
What do you think will happen if I print out the address of the
variable x and the address of the y reference?
Do you think that these addresses will be different?
Let's compile it.
And it seems that the reference, y, has the same address as the variable,
x, which makes sense because they point to the same place in memory.
These two contexts in which this character appears are
actually really hard to mistake for one another.
You will always know that this refers to the reference
declaration because you are declaring a reference.
It makes no sense that you are trying to find an address of
a variable that you have just created.
And also, this is an lvalue.
We'll talk about this in one of the following modules.
But basically,
you will never see a reference operator on the left side because you
cannot assign something else to a memory address.
And now, let's get back to the first example from the lesson.
This code now makes more sense.
Instead of declaring a regular variable or a pointer as a parameter,
this parameter is now defined as a reference.
So when we pass the variable x from the main function here,
this reference will become another name for that same place in memory,
even though this variable is in another scope.
And since this is just another name,
we don't have to use the dereference operator here to modify the data.
As you can see, this code looks much more cleaner and more readable.
It prevents you from making mistakes like forgetting
to make this variable a pointer,
passing an address or dereferencing a pointer inside of a function.
This code in compiled assembly will look basically the same
as the one we wrote with the pointers.
So you can think of references as a syntactic sugar,
which makes the code look cleaner.
Passing by reference is a very common occurrence in programming,
and this approach makes it so much more simple than using pointers.
I would usually cover this feature in the last module,
but this one is so useful and convenient that it simply makes no
sense to keep using pointers in this context,
not even as a learning tool, because in C++,
you will always use references to pass arguments by a reference.
This is not really that important when working with
primitive values like integers,
but we will use references with user‑defined objects in the following modules.