C++中*&和**&的含义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5789806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Meaning of *& and **& in C++
提问by sdffadsf
I found these symbols in a function declaration several times, but I don't know what they mean.
我在函数声明中多次发现这些符号,但我不知道它们是什么意思。
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];
}
回答by Naveen
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;
}
回答by Cat Plus Plus
First is a reference to a pointer, second is a reference to a pointer to a pointer. See also FAQ on how pointers and references differ.
第一个是对指针的引用,第二个是对指向指针的指针的引用。另请参阅有关指针和引用如何不同的常见问题解答。
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;
}
回答by sharptooth
That's passing a pointer by reference rather than by value. This for example allows altering the pointer (not the pointed-to object) in the function is such way that the calling code sees the change.
那是通过引用而不是通过值传递指针。例如,这允许更改函数中的指针(而不是指向的对象),以便调用代码看到更改。
Compare:
相比:
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
}
回答by Oswald
An int*
is a pointer to an int
, so int*&
must be a reference to a pointer to an int
. Similarly, int**
is a pointer to a pointer to an int
, so int**&
must be a reference to a pointer to a pointer to an int
.
Anint*
是指向 an 的指针int
,因此int*&
必须是指向 an 的指针的引用int
。类似地,int**
是指向指向 的指针的指针int
,因此int**&
必须是指向指向指向 的指针的引用int
。
回答by wheaties
To understand those phrases let's look at the couple of things:
要理解这些短语,让我们看看以下两件事:
typedef double Foo;
void fooFunc(Foo &_bar){ ... }
So that's passing a double by reference.
所以这是通过引用传递一个双精度值。
typedef double* Foo;
void fooFunc(Foo &_bar){ ... }
now it's passing a pointer to a double by reference.
现在它通过引用传递一个指向 double 的指针。
typedef double** Foo;
void fooFunc(Foo &_bar){ ... }
Finally, it's passing a pointer to a pointer to a double by reference. If you think in terms of typedefs like this you'll understand the proper ordering of the & and * plus what it means.
最后,它通过引用传递一个指向 double 的指针的指针。如果您考虑这样的类型定义,您就会理解 & 和 * 的正确顺序以及它的含义。
回答by Mahesh
*&
signifies the receiving the pointer by reference. It means it is an alias for the passing parameter. So, it affects the passing parameter.
*&
表示通过引用接收指针。这意味着它是传递参数的别名。因此,它会影响传递参数。
#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;
}
Output:
输出:
Before foo: 100
In foo: 50
After foo: 100
Before bar: 100
In bar: 80
After bar: 80
回答by Juan Chavarro
Typically, you can read the declaration of the variable from right to left. Therefore in the case of int *ptr;
, it means that you have a Pointer*
to an Integer variableint
. Also when it's declared int **ptr2;
, it is a Pointer variable*
to a Pointer variable*
pointing to an Integer variableint
, which is the same as "(int *)* ptr2;"
通常,您可以从右到左阅读变量的声明。因此,在的情况下int *ptr;
,这意味着你有一个指针*
到整型变量int
。此外,当它被声明时int **ptr2;
,它是一个Pointer 变量*
,指向一个Pointer 变量,*
指向一个Integer 变量int
,这与"(int *)* ptr2;"
Now, following the syntax by declaring int*& rPtr;
, we say it's a Reference&
to a Pointer*
that points to a variableof type int
. Finally, you can apply again this approach also for int**& rPtr2;
concluding that it signifies a Reference&
to a Pointer*
to a Pointer*
to an Integerint
.
现在,通过声明语法下面int*& rPtr;
,我们说这是一个参考&
的指针*
,它指向一个变量的类型int
。最后,你还可以再申请这种方法来int**& rPtr2;
断定它标志着一个参考&
的指针*
的指针*
到一个整数int
。
回答by Sachin Nale
This *&in theory as well as in practical its possible and called as reference to pointer variable. and it's act like same.
This *&combination is used in as function parameter for 'pass by' type defining. unlike ** can also be used for declaring a double pointer variable.
The passing of parameter is divided into pass by value, pass by reference, pass by pointer.
there are various answer about "pass by" types available. however the basic we require to understand for this topic is.
这个*&在理论上以及在实践中都是可能的,并被称为对指针变量的引用。它的行为就像一样。这个*&组合用作“pass by”类型定义的函数参数。不像 ** 也可以用于声明双指针变量。
参数的传递分为值传递、引用传递、指针传递。有各种关于“传递”类型的答案。然而,我们需要了解这个主题的基础知识。
pass by reference--> generally operates on already created variable refereed while passing to function e.g fun(int &a);
通过引用传递--> 通常在传递给函数时操作已经创建的变量引用,例如fun(int &a);
pass by pointer--> Operates on already initialized 'pointer variable/variable address' passing to function e.g fun(int* a);
通过指针传递--> 对传递给函数的已经初始化的“指针变量/变量地址”进行操作,例如fun(int* a);
auto addControl = [](SomeLabel** label, SomeControl** control) {
*label = new SomeLabel;
*control = new SomeControl;
// few more operation further.
};
addControl(&m_label1,&m_control1);
addControl(&m_label2,&m_control2);
addControl(&m_label3,&m_control3);
in the above example(this is the real life problem i came across) i am trying to init few pointer variable from the lambda function and for that we need to pass it by double pointer, so that comes with d-referencing of pointer for its all usage inside of that lambda + while passing pointer in function which takes double pointer, you need to pass reference to the pointer variable.
在上面的例子中(这是我遇到的现实生活中的问题)我试图从 lambda 函数初始化几个指针变量,为此我们需要通过双指针传递它,因此它带有指针的 d 引用在使用双指针的函数中传递指针时,该 lambda + 中的所有用法都需要传递对指针变量的引用。
so with this same thing reference to the pointer variable, *&this combination helps. in below given way for the same example i have mentioned above.
所以用同样的东西引用指针变量,*&这个组合有帮助。下面给出了我上面提到的相同示例的方式。
auto addControl = [](SomeLabel*& label, SomeControl*& control) {
label = new SomeLabel;
control = new SomeControl;
// few more operation further.
};
addControl(m_label1,m_control1);
addControl(m_label2,m_control2);
addControl(m_label3,m_control3);
so here you can see that you neither require d-referencing nor we require to pass reference to pointer variable while passing in function, as current pass by type is already reference to pointer.
所以在这里您可以看到,您既不需要 d 引用,也不需要在传入函数时传递对指针变量的引用,因为当前传递的类型已经是对指针的引用。
Hope this helps :-)
希望这可以帮助 :-)