C++ 函数声明中的 `*&` 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1426986/
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
What does `*&` in a function declaration mean?
提问by bastibe
I wrote a function along the lines of this:
我写了一个类似这样的函数:
void myFunc(myStruct *&out) {
out = new myStruct;
out->field1 = 1;
out->field2 = 2;
}
Now in a calling function, I might write something like this:
现在在调用函数中,我可能会这样写:
myStruct *data;
myFunc(data);
which will fill all the fields in data
. If I omit the '&
' in the declaration, this will not work. (Or rather, it will work only locally in the function but won't change anything in the caller)
这将填写data
. 如果我&
在声明中省略“ ”,这将不起作用。(或者更确切地说,它只会在函数中本地工作,但不会改变调用者的任何内容)
Could someone explain to me what this '*&
' actually does? It looks weird and I just can't make much sense of it.
有人可以向我解释这个“ *&
”实际上是做什么的?它看起来很奇怪,我只是无法理解它。
回答by unwind
The & symbol in a C++ variable declaration means it's a reference.
C++ 变量声明中的 & 符号表示它是一个引用。
It happens to be a reference to a pointer, which explains the semantics you're seeing; the called function can change the pointer in the calling context, since it has a reference to it.
它恰好是对指针的引用,它解释了您所看到的语义;被调用函数可以更改调用上下文中的指针,因为它有一个对它的引用。
So, to reiterate, the "operative symbol" here is not *&
, that combination in itself doesn't mean a whole lot. The *
is part of the type myStruct *
, i.e. "pointer to myStruct
", and the &
makes it a reference, so you'd read it as "out
is a reference to a pointer to myStruct
".
所以,重申一下*&
,这里的“操作符号”不是,这种组合本身并不意味着很多。该*
是类型的一部分myStruct *
,即“指针myStruct
”,并&
使它成为一个参考,所以你把它读作“out
是一个指针引用myStruct
”。
The original programmer could have helped, in my opinion, by writing it as:
在我看来,最初的程序员可以通过将其编写为:
void myFunc(myStruct * &out)
or even (not my personal style, but of course still valid):
甚至(不是我的个人风格,但当然仍然有效):
void myFunc(myStruct* &out)
Of course, there are many other opinions about style. :)
当然,关于风格还有很多其他的意见。:)
回答by Adriaan
In C and C++, & means call by reference; you allow the function to change the variable. In this case your variable is a pointer to myStruct type. In this case the function allocates a new memory block and assigns this to your pointer 'data'.
在 C 和 C++ 中,& 表示按引用调用;您允许函数更改变量。在这种情况下,您的变量是指向 myStruct 类型的指针。在这种情况下,该函数分配一个新的内存块并将其分配给您的指针“数据”。
In the past (say K&R) this had to be done by passing a pointer, in this case a pointer-to-pointer or **. The reference operator allows for more readable code, and stronger type checking.
在过去(比如 K&R),这必须通过传递一个指针来完成,在这种情况下是一个指针到指针或 **。引用运算符允许更易读的代码和更强的类型检查。
回答by Martin York
This looks like you are re-implementing a constructor!
看起来您正在重新实现构造函数!
Why not just create the appropriate constructor?
Note in C++ a struct is just like a class (it can have a constructor).
为什么不创建合适的构造函数呢?
注意在 C++ 中,结构就像一个类(它可以有一个构造函数)。
struct myStruct
{
myStruct()
:field1(1)
,field2(2)
{}
};
myStruct* data1 = new myStruct;
// or Preferably use a smart pointer
std::auto_ptr<myStruct> data2(new myStruct);
// or a normal object
myStruct data3;
回答by Johannes Schaub - litb
It may be worthwhile to explain why it's not &*
, but the other way around. The reason is, the declarations are built recursively, and so a reference to a pointer builds up like
解释为什么不是&*
,而是相反的原因可能是值得的。原因是,声明是递归构建的,因此对指针的引用构建如下
& out // reference to ...
* (& out) // reference to pointer
The parentheses are dropped since they are redundant, but they may help you see the pattern. (To see why they are redundant, imagine how the thing looks in expressions, and you will notice that first the address is taken, and then dereferenced - that's the order we want and that the parentheses won't change). If you change the order, you would get
括号被删除,因为它们是多余的,但它们可以帮助您查看模式。(要了解为什么它们是多余的,请想象一下表达式中的内容,您会注意到首先获取地址,然后取消引用 - 这是我们想要的顺序并且括号不会改变)。如果你改变顺序,你会得到
* out // pointer to ...
& (* out) // pointer to reference
Pointer to reference isn't legal. That's why the order is *&
, which means "reference to pointer".
指向引用的指针是不合法的。这就是为什么顺序是*&
,这意味着“对指针的引用”。
回答by Graphics Noob
Like others have said, the & means you're taking a reference to the actual variable into the function as opposed to a copy of it. This means any modifications made to the variable in the function affect the original variable. This can get especially confusing when you're passing a pointer, which is already a reference to something else. In the case that your function signature looked like this
就像其他人所说的那样, & 意味着您将实际变量的引用引入到函数中,而不是它的副本。这意味着对函数中的变量所做的任何修改都会影响原始变量。当您传递一个指针时,这可能会特别令人困惑,该指针已经是对其他内容的引用。如果您的函数签名看起来像这样
void myFunc(myStruct *out);
What would happen is that your function would be passed a copy of the pointer to work with. That means the pointer would point at the same thing, but would be a different variable. Here, any modifications made to *out
(ie what out points at) would be permanent, but changes made to out
(the pointer itself) would only apply inside of myFunc
. With the signature like this
会发生的情况是,您的函数将传递一个要使用的指针副本。这意味着指针将指向同一事物,但将是不同的变量。在这里,对*out
(即 out 指向的内容)所做的任何修改都将是永久性的,但对out
(指针本身)所做的更改仅适用于myFunc
. 有了这样的签名
void myFunc(myStruct *&out);
You're declaring that the function will take a reference to the original pointer. Now any changes made to the pointer variable out
will affect the original pointer that was passed in.
您声明该函数将引用原始指针。现在对指针变量所做的任何更改out
都会影响传入的原始指针。
That being said, the line
话虽如此,这条线
out = new myStruct;
is modifying the pointer variable out
and not *out
. Whatever out
used to point at is still alive and well, but now a new instance of myStruct has been created on the heap, and out
has been modified to point at it.
正在修改指针变量out
而不是*out
. out
以前指向的任何东西仍然有效,但是现在在堆上创建了一个 myStruct 的新实例,并out
已修改为指向它。
回答by Nikolai Fetissov
In C++ it's a reference to a pointer, sort of equivalent to a pointer to pointer in C, so the argument of the function is assignable.
在 C++ 中,它是对指针的引用,相当于 C 中指向指针的指针,因此函数的参数是可分配的。
回答by Michael Kristofik
As with most data types in C++, you can read it right-to-left and it'll make sense.
与 C++ 中的大多数数据类型一样,您可以从右到左阅读它,这很有意义。
myStruct *&out
out
is a reference (&
) to a pointer (*
) to a myStruct
object. It must be a reference because you want to change what out
points at (in this case, a new myStruct
).
out
是&
对指向对象的指针 ( *
)的引用 ( ) myStruct
。它必须是一个引用,因为您想更改out
指向的内容(在本例中为 a new myStruct
)。
回答by GG.
MyClass *&MyObject
MyClass *&MyObject
Here MyObject is reference to a pointer of MyClass. So calling myFunction(MyClass *&MyObject) is call by reference, we can change MyObject which is reference to a pointer. But If we do myFunction( MyClass *MyObject) we can't change MyObject because it is call by value, It will just copy address into a temporary variable so we can change value where MyObject is Pointing but not of MyObject.
这里 MyObject 是对 MyClass 指针的引用。所以调用 myFunction(MyClass *&MyObject) 是通过引用调用,我们可以改变 MyObject ,它是对指针的引用。但是如果我们做 myFunction(MyClass *MyObject) 我们不能改变 MyObject 因为它是按值调用的,它只会将地址复制到一个临时变量中,这样我们就可以改变 MyObject 指向而不是 MyObject 的值。
so in this case writer is first assigning a new value to out thats why call by reference is necessary.
所以在这种情况下,作者首先分配一个新值,这就是为什么需要通过引用调用。