C++ 引用

在 C++ 中,我们使用引用来创建变量的别名。我们可以使用引用变量来访问或修改变量。


创建 C++ 引用

我们使用 and 符号来创建引用。例如,

string& ref_city = city;

这里,

  • string - 变量的数据类型
  • & - 表示我们正在创建引用
  • ref_city - 引用变量的名称
  • city - 创建引用的变量

示例:C++ 引用

#include <iostream>

using namespace std;

int main() {

    string city = "Paris";

    // create a reference to the variable
    string& ref_city = city;
    
    // display the variable
    cout << "Variable Value: " << city << endl;
    cout << "Reference Value: " << ref_city << endl;

    return 0;
}

输出

Variable Value: Paris
Reference Value: Paris

在上面的示例中,我们使用了引用变量 ref_city 来显示变量 city 的值。


通过引用修改变量

我们可以通过简单地将一个新值赋给引用变量来修改变量。例如,

#include <iostream>

using namespace std;

int main() {

    string city = "Paris";

    // create a reference to the variable
    string& ref_city = city;
    
    // display the variable
    cout << "Variable Value = " << city << endl;
    cout << "Reference Value = " << ref_city << endl;

    // modify the variable using reference
    ref_city = "New York";

    // display the variable
    cout << endl << "After Modification: " << endl;
    cout  << "Variable Value = " << city << endl;
    cout << "Reference Value = " << ref_city << endl;

    return 0;
}

输出

Variable Value: Paris
Reference Value: Paris

After Modification:
Variable Value: = New York
Reference Value: New York

要点

1. & 符号的放置

我们在创建引用时,可以将 & 符号放在数据类型或变量旁边。但是,标准的做法是将符号与数据类型一起使用。例如,

// create a variable
string city = "Paris";

// valid but not a standard practice
string &ref_city = city;

// valid and a standard practice
sring& ref_city = city;

2. 引用的初始化

我们必须在声明时初始化引用。

// create a variable
string city = "Paris";

// incorrect code [reference not initialized]
string& ref_city;
ref_city = city;

// correct code
string& ref_city = city;

3. 引用与新变量

一旦我们创建了一个变量的引用,就不能将其更改为引用另一个变量。例如,

#include <iostream>

using namespace std;

int main() {

    string city1 = "Paris";

    // create a reference to the variable
    string& ref_city = city1;
    
    // display the variable
    cout << "city1 = " << city1 << endl;
    cout << "ref_city = " << ref_city << endl;
    
    string city2 = "New York";
    
    // trying to modify the ref_city reference variable to refer to city2
    // but it assigns the value of city2 to the variable city1
    ref_city = city2;

    // display the variables
    cout << endl << "city1 = " << city1 << endl;
    cout << "city2 = " << city2 << endl;
    cout << "ref_city = " << ref_city << endl;

    return 0;
}

输出

city1 = Paris
ref_city = Paris

city1 = New York
city2 = New York
ref_city = New York

在这里,当我们尝试在行 my_city = city2; 中将引用变量 ref_city 更改为引用变量 city2 时,引用变量 my_city 并未更改为引用 city2

相反,由于引用变量 ref_city 引用变量 city1,因此 city2 的值被赋给了变量 city1


常见问题

引用的类型有哪些?

C++ 中主要有两种类型的引用:

  • lvalue 引用
  • rvalue 引用

lvalue 引用

lvalue 是一个变量或对象,它可能在其创建的表达式之外存在。例如,

int a = 5;

在这里,我们在该表达式中创建了变量 a,但它可以在程序的任何其他表达式中使用。事实上,它可以在声明它的作用域中使用。

顾名思义,lvalue 引用用于引用 lvalue(变量或对象)。

rvalue 引用

rvalue 是一个值(如字面量、运算符结果、函数返回值等),它在其创建的表达式之外不存在。

int a = 5;

在这里,5rvalue。它不能从另一个表达式访问。

注意:本文中使用的所有引用都是 lvalue 引用。rvalue 引用通常用于移动语义,这超出了本教程的范围。

引用和指针之间有什么区别?

1. 引用是变量的别名,而指针是一个不同的变量,它存储变量的内存地址。例如,

cout << "s = " << s << endl;
string s = "Paris";

string* pointer_to_s = &s;
// prints the memory address of s
cout << "pointer_to_s = " << pointer_to_s << endl;
string& ref_s = s;
// prints the string s
cout << "ref_s = " << ref_s << endl;

2. 我们使用解引用运算符 * 来访问指针指向的值。但是,我们可以直接使用引用而无需任何运算符。

// print the value of the variable using pointer
cout << "*pointer_to_s = " << *pointer_to_s << endl;

// print the value of the variable using reference
cout << "ref_s = " << ref_s << endl;

3. 我们可以更改指针以指向另一个变量。例如,

// create variable city1 and city2
string city1 = "New York";
string city2 = "Las Vegas";

// create a pointer to city1
string* point_city = &city1;

// print the value pointed by point_city
cout << "*point_city = " << *point_city << endl;

// change the pointer to point to city2
point_city = &city2;
cout << "*point_city = " << *point_city << endl;

但是,我们不能将引用更改为引用另一个变量。


另请阅读

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战