References
Now, we'll learn what references are and how they differ from pointers.
We'll cover the following
A reference is an alias for an existing variable. It can be created using the
&
operator.
Once created, the reference can be used instead of the actual variable. Altering the value of the reference is equivalent to altering the referenced variable.
Note that we don’t have to worry about the *
operator when de-referencing a variable reference. For example, lines 7, 11 and 14 in the following program.
Note that we are using
std::cout
every time we want to display something to the screen, instead ofcout
since we haven’t used theusing namespace std
statement. Similarly, we are usingstd::endl
instead ofendl
. Both options work the same, but avoiding theusing namespace
statement has its advantages.