*&和**&在C ++中 [英] Meaning of *& and **& in C++

查看:107
本文介绍了*&和**&在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在函数声明中发现了这些符号几次,但我不知道它们的意思。

I found these symbols in a function declaration several times, but I don't know what they mean.

示例: p>

Example:

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];
}


推荐答案

参考。所以在第一种情况下,你通过引用取指针参数,所以你对指针的值做的任何修改都反映在函数之外。第二个是类似于第一个,唯一的区别是它是一个双指针。请参阅此示例:

That is taking the parameter by reference. So in the first case you are taking a pointer parameter by reference so whatever modification you do to the value of the pointer is reflected outside the function. Second is the simlilar to first one with the only difference being that it is a double pointer. See this example:

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;
}

这篇关于*&amp;和**&amp;在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆