In C++, a reference variable is an alias for something else, that is, another name for an already existing variable.
So suppose we make Sonny a reference to someone named Songqiao. You can refer to the person as either Sonny or Songqiao.
Let’s take a look at how we can do this with code. Suppose we have an
int variable already called
songqiao, we can create an alias to it by using the
& sign in the declaration:
int &sonny = songqiao;
So here, we made
sonny a reference to
Now when we make changes to
sonny (add 1, subtract 2, etc),
songqiao also changes.
Two things to note about references:
We have a variable called
soda declared and initialized to
int variable called
pop and make it a reference to
Let’s add 1 to
Print out out
Are they the same?