c++ - C++ 中 *& 和 **& 的含义

我在函数声明中多次找到这些符号,但我不知道它们的含义。

示例:

void raccogli_dati(double **& V, double **p, int N) { 
  int ultimo = 3; 
  V = new double * [N/2]; 
  for(int i=0; i < N/2; i++) { 
    V[i] = new double[N/2], std :: clog << "digita " << N/2 - i
                 << " valori per la parte superiore della matrice V: "; 
    for(int j=i; j < N/2; j++) 
      std :: cin >> V[i][j], p[ultimo++][0] = (V[i][j] /= sqrt(p[i][0]*p[j][0]));
  } 
  for(int i=1; i < N/2; i++) 
    for(int j=0; j < i; j++) 
       V[i][j] = V[j][i];
}

最佳答案

这是通过引用获取参数。因此,在第一种情况下,您通过引用获取指针参数,因此您对指针值所做的任何修改都会反射(reflect)在函数之外。第二个与第一个相似,唯一的区别是它是一个双指针。看这个例子:

void pass_by_value(int* p)
{
    //Allocate memory for int and store the address in p
    p = new int;
}

void pass_by_reference(int*& p)
{
    p = new int;
}

int main()
{
    int* p1 = NULL;
    int* p2 = NULL;

    pass_by_value(p1); //p1 will still be NULL after this call
    pass_by_reference(p2); //p2 's value is changed to point to the newly allocate memory

    return 0;
}

关于c++ - C++ 中 *& 和 **& 的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5789806/

相关文章:

c++ - 如何以编程方式确定表达式在 C++ 中是右值还是左值?

c++ - 私有(private)构造函数

c++ - 为什么要使用断言?

c++ - 从构造函数的初始化列表中捕获异常

c++ - 三向比较运算符与减法有何不同?

c++ - 不允许指向不完整类类型的指针

c++ - C++ 中的常量和编译器优化

c++ - 在没有实例的情况下获取 std::array 的大小

c++ - 字符串文字中的符号\0 是什么意思?

c++ - 是否可以防止对象的堆栈分配并只允许使用 'new' 对其进行实例化?