A reference variable is a "reference" to an existing variable, and it is created with the &
operator:
string food = "Pizza"; // food variable
string &meal = food; // reference to food
Now, we can use either the variable name food
or the reference name meal
to refer to the food
variable:
string food = "Pizza";
string &meal = food;
cout << food << "\n"; // Outputs Pizza
cout << meal << "\n"; // Outputs Pizza
Try it Yourself »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!