c++的引用和解引用

C++ reference and dereference

本文关键字:引用 和解 c++      更新时间:2023-10-16

我有一个问题要问你。

void static splitArray(int *A, int size, int ind1, int ind2, int *&first, int &firstSize, int *&second, int &secondSize)
{
    firstSize = ind2 - ind1 + 1;
    secondSize = size - firstSize;
    first = new int[firstSize];
    second = new int[secondSize];
    int cnt1 = 0, cnt2 = 0;
    for (int i = 0; i < size; i++){ 
        if ((i >= ind1) && (i <= ind2)){
            first[cnt1] = A[i];
            cnt1++;
        } 
        else {
            second[cnt2] = A[i];
            cnt2++;
        }
    }

该函数将输入数组拆分为参数中给定的数字索引,并将剩余部分合并并填充参数中的数组。但在形参中,引用和解引用操作符一起使用。(*,)这不是应该互相抵消吗?它是如何工作的?你能帮帮我吗?

考虑一个简单的例子

int x;
int &rx = x;
int *p;
int * &rp = p;

声明
int *&first

是对int *

类型对象的引用的声明。

在函数体中,第一个和第二个变量被改变。

first = new int[firstSize];
second = new int[secondSize];

因为它们是对参数的引用,所以这些更改实际上是对作为参数传递给函数的原始对象进行的。

这里的内容是将指向int类型的指针的引用传递给函数。

退一步说,如果你只把"指向int型的指针"传递给函数,一旦进入函数,你就可以改变指针指向的对象(也就是说,你可以改变指针指向的内存位置)。

在这种情况下,因为你传递了一个指向int型指针的引用(把它看作是另一个间接的级别,也就是说,这里的间接级别相当于间接的级别,就好像你有一个指向int型指针的指针),你实际上可以改变指针本身的内存位置

这看起来很奇怪,但可以使一些代码看起来更简单。考虑两种创建数组的等效方法:

void array_byref(int*& p)
{
  p = (int*) malloc(sizeof(int)*3);
  p[0] = 0;
  p[1] = 1;
  p[2] = 2;
}
void array_byptr(int** p)
{
  *p = (int*) malloc(sizeof(int)*3);
  (*p)[0] = 0;
  (*p)[1] = 1;
  (*p)[2] = 2;
}

上面的例子看起来比下面的整洁,因为它结合了指针和引用。但我并没有真正看到"在野外"的使用,所以我认为这不是一个常见的或好的实践。

*&字符做两件事:

  1. they 声明指针和引用类型

    int i = 7;   // this is just an int
    int *p;      // this is a pointer-to-int
    int &r = i;  // this is a reference-to-int;
    
  2. 和它们操作对指针解引用并取对象的地址(分别)

    int i = *p; // dereference pointer-to-int p (and get an integer value)
    p = &i;     // take the address of int i (and get a pointer-to-int)
    

我们可以在一些实际的代码中显示它们:

// the function header is declaring the arguments,
// so here * and & are part of the type
int foo(int i,    // i is an int
        int *p,   // p is a pointer-to-int
        int &r,   // r is a reference-to-int
        int *&rp  // rp is a reference to a pointer-to-int
        )
{
    // the function body is executing statements,
    // so * and & here mean the operators
    *p = i; // operator* dereferences pointer p,
            // then we store the value of i at that address
    r = i;  // references do this automatically
    rp = p; // we just changed the caller's pointer-to-int,
            // via the reference, so it points to the same int as p
    p = &i; // then we used operator& to take the address of i, and
            // point our local copy of pointer-to-int p there
    // one last declaration, for old time's sake
    int &l = *p; // a reference to the integer p points at
    return l;
}

你可以运行这个来查看效果:

#include <iostream>
using namespace std;
int main() {
   const int c = 42;
   int i = 7;
   int *in = &i;
   int **out = 0;
   int result = foo(c, in, i, out);
   cout << "c   = " << c << endl;
   cout << "i   = " << i << endl;
   cout << "in  = " << in << ", *in = " << *in << endl;
   cout << "out = " << out << ", *out = " << *out << endl;
}