C++中的引用用法指南和代码示例

2021年3月10日16:07:59 发表评论 639 次浏览

本文概述

当将变量声明为引用时, 它将成为现有变量的替代名称。通过在声明中添加"&", 可以将变量声明为引用

CPP

#include<iostream>
using namespace std;
 
int main()
{
   int x = 10;
 
   // ref is a reference to x.
   int & ref = x;
 
   // Value of x is now changed to 20
   ref = 20;
   cout << "x = " << x << endl ;
 
   // Value of x is now changed to 30
   x = 30;
   cout << "ref = " << ref << endl ;
 
   return 0;
}

输出如下:

x = 20
ref = 30

应用范围: 

  1. 修改函数中传递的参数:如果函数收到对变量的引用, 则可以修改该变量的值。例如, 以下程序变量使用引用进行交换。

CPP

#include<iostream>
using namespace std;
 
void swap ( int & first, int & second)
{
     int temp = first;
     first = second;
     second = temp;
}
 
int main()
{
     int a = 2, b = 3;
     swap( a, b );
     cout << a << " " << b;
     return 0;
}

输出如下:

3 2

1.避免 大型结构的副本:想象一个必须接收一个大对象的函数。如果我们通过它而没有引用, 则会创建它的新副本, 这会浪费CPU时间和内存。我们可以使用引用来避免这种情况。

CPP

struct Student {
    string name;
    string address;
    int rollNo;
}
 
// If we remove & in below function, a new
// copy of the student object is created.
// We use const to avoid accidental updates
// in the function as the purpose of the function
// is to print s only.
void print( const Student &s)
{
     cout << s.name << "  " << s.address << "  " << s.rollNo;
}

2.在"每个循环"中修改所有对象:我们可以在每个循环中使用引用来修改所有元素

CPP

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
     vector< int > vect{ 10, 20, 30, 40 };
 
     // We can modify elements if we
     // use reference
     for ( int &x : vect)
         x = x + 5;
 
     // Printing elements
     for ( int x : vect)
        cout << x << " " ;
 
     return 0;
}

3.对于每个循环, 避免 对象副本:我们可以在每个循环中使用引用, 以避免在对象较大时复制单个对象。

CPP

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
     vector<string> vect{ "lsbin practice" , "lsbin write" , "lsbin ide" };
 
     // We avoid copy of the whole string
     // object by using reference.
     for ( const auto &x : vect)
        cout << x << endl;
 
     return 0;
}

参考与指针

引用和指针都可以用于更改另一个函数内部的一个函数的局部变量。当作为参数传递给函数或从函数返回时, 它们都还可以用于保存大对象的副本, 以提高效率。尽管有上述相似之处, 引用和指针之间还是存在以下差异。

1.可以将指针声明为void, 但是引用永远不能为void例如

int a = 10;
void* aa = &a;. //it is valid
void &ar = a; // it is not valid

感谢Shweta Bansal补充了这一点。

2.指针变量具有n级/多级间接寻址, 即单指针, 双指针, 三指针。而参考变量仅具有一个/单个间接级别。以下代码揭示了上述要点:

C ++

#include <iostream>
using namespace std;
 
int main() {
     int i=10; //simple or ordinary variable.
     int *p=&i; //single pointer
     int **pt=&p; //double pointer
     int ***ptr=&pt; //triple pointer
     // All the above pointers differ in the value they store or point to.
     cout << "i=" << i << "\t" << "p=" << p << "\t"
          << "pt=" << pt << "\t" << "ptr=" << ptr << "\n" ;
     int a=5; //simple or ordinary variable
     int &S=a;
     int &S0=S;
     int &S1=S0;
     cout << "a=" << a << "\t" << "S=" << S << "\t"
          << "S0=" << S0 << "\t" << "S1=" << S1 << "\n" ;
     // All the above references do not differ in their values
     // as they all refer to the same variable.
  }

引用没有指针强大

1)创建引用后, 以后就不能再引用另一个对象了;它无法重新放置。这通常是通过指针完成的。

2)引用不能为NULL。指针通常被设置为NULL, 以指示它们没有指向任何有效的对象。

3)引用必须在声明时进行初始化。指针没有这种限制

由于上述限制, C ++中的引用不能用于实现链接列表, 树等数据结构。在Java中, 引用没有上述限制, 可以用于实现所有数据结构。 Java中引用功能更强大是Java不需要指针的主要原因。

参考更安全, 更易于使用:

1)更安全:

由于必须先初始化引用, 所以像

野指针

不太可能存在。仍然有引用可能没有指向有效位置(请参阅以下练习中的问题5和6)

2)易于使用:

引用不需要解引用运算符即可访问值。它们可以像普通变量一样使用。仅在声明时才需要"&"运算符。此外, 对象引用的成员可以使用点运算符(。)进行访问, 这与需要使用箭头运算符(->)来访问成员的指针不同。

结合上述原因, 在诸如复制构造函数参数之类的地方很少有不能使用指针的地方。必须使用引用在复制构造函数中传递参数。同样, 必须使用引用来重载某些运算符, 例如++。

行使:

预测以下程序的输出。如果存在编译错误, 请修复它们。

问题1

CPP

#include<iostream>
using namespace std;
 
int &fun()
{
     static int x = 10;
     return x;
}
int main()
{
     fun() = 30;
     cout << fun();
     return 0;
}

问题2 

CPP

#include<iostream>
using namespace std;
 
int fun( int &x)
{
     return x;
}
int main()
{
     cout << fun(10);
     return 0;
}

问题3 

CPP

#include<iostream>
using namespace std;
 
void swap( char * &str1, char * &str2)
{
   char *temp = str1;
   str1 = str2;
   str2 = temp;
}
 
int main()
{
   char *str1 = "GEEKS" ;
   char *str2 = "FOR GEEKS" ;
   swap(str1, str2);
   cout<< "str1 is " <<str1<<endl;
   cout<< "str2 is " <<str2<<endl;
   return 0;
}

问题4 

CPP

#include<iostream>
using namespace std;
 
int main()
{
    int x = 10;
    int *ptr = &x;
    int &*ptr1 = ptr;
}

问题5 

CPP

#include<iostream>
using namespace std;
 
int main()
{
    int *ptr = NULL;
    int &ref = *ptr;
    cout << ref;
}

问题6 

CPP

#include<iostream>
using namespace std;
 
int &fun()
{
     int x = 10;
     return x;
}
int main()
{
     fun() = 30;
     cout << fun();
     return 0;
}

相关文章 :

  • C ++中的指针与引用
  • 我们什么时候通过引用或指针传递参数?
  • 引用可以引用C ++中的无效位置吗?
  • 在C ++中通过指针传递Vs通过引用传递

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请发表评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: