*&和**&在C++中的含义

sdf*_*dsf 53 c++ syntax pointers symbols reference

我在函数声明中多次发现这些符号,但我不知道它们是什么意思.

例:

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];
}
Run Code Online (Sandbox Code Playgroud)

Nav*_*een 86

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

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;
}
Run Code Online (Sandbox Code Playgroud)


Cat*_*lus 14

第一个是对指针的引用,第二个是对指针的指针的引用.另请参阅有关指针和引用如何不同的常见问题.

void foo(int*& x, int**& y) {
    // modifying x or y here will modify a or b in main
}

int main() {
    int val = 42;
    int *a  = &val;
    int **b = &a;

    foo(a, b);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

  • 我知道我不应该这样做,但我赞成这个答案只是因为它包含了一个指针 - 指针指向一个指向生命意义的指示.不知何故,我认为我们距离Deep Thought只有几年的时间. (3认同)

sha*_*oth 12

那是通过引用而不是值传递指针.这例如允许在函数中改变指针(而不是指向对象),使得调用代码看到改变.

相比:

void nochange( int* pointer ) //passed by value
{
   pointer++; // change will be discarded once function returns
}

void change( int*& pointer ) //passed by reference
{
   pointer++; // change will persist when function returns
}
Run Code Online (Sandbox Code Playgroud)


Mah*_*esh 7

*&表示通过引用接收指针。这意味着它是传递参数的别名。因此,它会影响传递参数。

#include <iostream>
using namespace std;

void foo(int *ptr)
{
    ptr = new int(50);    // Modifying the pointer to point to a different location
    cout << "In foo:\t" << *ptr << "\n"; 
    delete ptr ;
}

void bar(int *& ptr)
{
    ptr = new int(80);    // Modifying the pointer to point to a different location
    cout << "In bar:\t" << *ptr << "\n"; 
    // Deleting the pointer will result the actual passed parameter dangling
}
int main()
{
    int temp = 100 ;
    int *p = &temp ;

    cout << "Before foo:\t" << *p << "\n";
    foo(p) ;
    cout << "After foo:\t" << *p << "\n";

    cout << "Before bar:\t" << *p << "\n";
    bar(p) ;
    cout << "After bar:\t" << *p << "\n";

    delete p;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

Before foo: 100
In foo: 50
After foo:  100
Before bar: 100
In bar: 80
After bar:  80
Run Code Online (Sandbox Code Playgroud)


小智 7

通常,您可以从右到左阅读变量的声明。因此,在这种情况下int *ptr;,这意味着您有一个指向整数变量的指针 。另外,当它被声明时,它是一个指针变量,指向一个指向整数变量的指针变量,这与* intint **ptr2; * * int"(int *)* ptr2;"

现在,按照声明的语法int*& rPtr;,我们说它是指向指向类型变量指针的引用 。最后,您可以再次应用此方法来得出结论:它表示对指向Integer指针引用& *intint**& rPtr2; & * * int

  • +1提示从右到左阅读声明:这大大加快了理解速度,也解释了为什么C++构造的解释有时会很困难(至少对于那些通常从左到右阅读的人来说) (3认同)

Osw*_*ald 5

int*是一个指针int.然后int*&必须是对指针的引用int.类似地,int**是指向一个指针的指针int,然后是int**&必须是指向指向一个指针的指针int**&.