C++ References
Creating References
A reference variable is an alias for an existing variable. It is created using the &
operator:
string food = "Pizza"; // food variable
string &meal = food;
// reference to food
Now, you can use either food
or meal
to refer to the same value:
Example
string food = "Pizza";
string &meal = food;
cout << food << "\n";
// Outputs Pizza
cout << meal << "\n"; //
Outputs Pizza
Try it Yourself »
Note: Both food
and meal
refer to the same memory location. Changing one affects the other.