C++ 引用


2022年10月6日, Learn eTutorial
2063

本教程将通过简单的示例介绍 C++ 中的引用主题。

变量被声明为引用时,它将通过指向现有内存而非生成新内存,成为现有变量的别名。这些变量也称为别名变量链接变量指针的问题在于我们将来可以更改现有变量的地址,这会使其不安全。为了解决这个问题,使用了引用变量

众所周知,C++ 主要支持两种类型的变量

普通变量是一种主要保存某种类型值的变量。例如,假设我们构造或创建了一个 int 类型的变量,这意味着该变量将能够保存整数类型的值。

包含另一个变量地址的变量称为指针。该指针指向的值可以通过解引用来检索。

引用是 C++ 支持的一种不同类型的变量。作为另一个变量的别名的变量称为引用

引用和指针的三个主要区别如下

  • 不允许空引用。您应该始终能够假定引用指的是有效的存储空间。
  • 假设一旦引用被初始化为一个对象,就不能更改它以引用不同的对象。任何时候,指针都可以指向任何其他对象。
  • 创建引用时,应该对其进行初始化。任何时候,指针都可以被初始化。

如何在 C++ 中创建引用?

可以使用与号(&)运算符来创建引用。假设我们正在创建一个变量,它占用一些内存空间。我们有能力为该变量创建一个引用;因此,我们可以使用变量名或引用来检索原始变量。

语法

data_type &refernce_variabe=value_variable;
 

例如

int a=7;
int &z=a;//now here you can see both z and a have the same address
 

前面示例中的变量“a”和“z”指向相同的内存地址。因此,“z”现在已成为“a”的别名。对“a”的任何修改也会影响“z”,反之亦然。

例如:让我们编写一个 C++ 程序,它将使用 int 和 double 的引用。


#include <iostream>
 
using namespace std;
 
int main () {
   // declare the simple variables
   int    i;
   double d;
 
   // declare the reference variables
   int&    r = i;
   double& s = d;
   
   i = 7;
   cout << " The value of i : " << i << endl;
   cout << "The value of i reference : " << r  << endl;
 
   d = 11.7;
   cout << "The Value of d will be : " << d << endl;
   cout << "The Value of d reference will be : " << s  << endl;
   
   return 0;
}

输出

The value of i : 7
The value of i reference : 7
The Value of d will be : 11.7
The Value of d reference will be : 11.7

C++ 提供两种不同类型的引用

  1. 对非 const 值的引用
  2. 引用作为别名

1. 对非 const 值的引用

使用引用类型变量和 & 运算符可以声明它。


#include <iostream>  
using namespace std;  
int main()  
{  
int a=100;  
int &value=a;  
std::cout << value << std::endl;  
return 0;  
}

输出

100

2. 引用作为别名

被引用的变量的另一个名称是引用作为别名。

例如,


int a=50;   // 'a' is a variable.  
int &b=a; // 'b' will reference to a.  
int &c=a; // 'c' will reference to a.  
 

现在让我们编写一个简单的 C++ 程序来使其更清晰


#include <iostream>  
using namespace std;  
int main()  
{  
int a=100; //  initialization of the variable
int &b=a;  
int &c=a;  
std::cout << " The value of a is :" <<a<< std::endl;  
std::cout << " The Value of b is :" <<b<< std::endl;  
std::cout << "The Value of c is :" <<c<< std::endl;  
return 0;
} 

输出

The value of a is :100
The Value of b is :100
The Value of c is :100

在上面的代码中,创建了一个名为“a”的变量,其值为“100”。我们的两个引用变量 b 和 c 都引用相同的变量“a”并已声明。因此,我们可以说变量“b”和“c”可以访问变量“a”。

引用的属性是什么?

C++ Reference
  1. 初始化

    在声明时,它必须被初始化

    
    #include <iostream>  
    using namespace std;  
    int main()  
    {  
    int a=50; //  initialization of the variable 
    int &b=a; // b will reference to a  
    std::cout << "Then the value of a is " <<b<< std::endl;  
    return 0;  
    }

    输出

    Then the value of a is 50
    

    在上面的代码中,我们创建了一个引用变量,即“b”。在声明时,'a'变量被赋值给'b'。如果我们在声明时没有赋值,那么代码将如下所示

    
    int &b;  
    &b=a;    
     
    

    由于在声明时未赋值 'a',上面的代码将在编译时失败。

  2. 重新赋值

    这表示引用变量不能更改/修改,因为它不能被重新赋值。

    
    #include <iostream>  
    using namespace std;  
    int main()  
    {  
    int x=100; //  initialization of  variable
    int z=7oo;  
    int &y=x; // y reference to x  
    int &y=z; // y reference to z, but it will surely throws a compile-time error.  
    return 0;
    } 
    

    输出

    Compilation error occurs
    

    在上面的代码中,引用变量“y”用于引用变量“x”,然后将“z”赋值给“y”。由于引用变量无法进行此重新赋值,因此会生成编译时错误。

  3. 函数参数

    也可以将引用作为函数参数传递。它充当参数的别名,但不会复制参数。由于它不复制参数,因此提高了性能。

    例如

    
    #include <iostream>  
    using namespace std;  
    int main()  
    {  
      int a=90; // initialization  of  variable
      int b=100; // initialization of  variable
      swap(a, b); // function calling  
      std::cout << " The value of a is :" <<a<< std::endl;  
      std::cout << "The value of b is :" <<b<< std::endl;  
      return 0;  
    }  
    void swap(int &p, int &q) // function definition  
    {  
      int temp; //  declaration of the variable 
      temp=p;  
      p=q;  
      q=temp;  
    }
    

    输出

    The value of a is :100
    The value of b is :90
    

    我们正在切换上面代码中“a”和“b”的值。swap() 函数已将变量“a”和“b”作为输入接收。在 swap() 函数中,“p”代表“a”,“q”代表“b”。当我们切换“p”和“q”的值时,这同样会切换“a”和“b”的值。

  4. 引用作为快捷方式

    引用使我们能够轻松访问嵌套数据。

    
    #include <iostream>  
    using namespace std;  
    struct profile  
    {  
    int id;  
    };  
    struct Student 
    {  
    profile p;  
    };  
    int main()  
    {  
    Student e;  
    int &ref=e.p.id;  
    ref=27;  
    std::cout << e.p.id << std::endl;  
    }
    

    输出

    27
    

    在前面的代码中,我们正在尝试获取学生个人资料结构体的“id”。通常,我们使用语句 e.p.id 来访问此成员,但如果我们需要多次访问,这将是一项非常困难的任务。为了防止这种情况,我们建立了一个名为 ref 的引用变量,它是“e.p.id”的另一个名称。