在 C++ 中通过引用调用
实际上通过引用函数在调用中传递了什么?
void foo(int &a,int &b)
当我写下
foo(p,q)
实际传递给函数的内容时。是p和q的地址吗?
What is actually passed in call by reference to a function?
void foo(int &a,int &b)
when I write
foo(p,q)
what is actually passed to the function. Is it the address of p and q?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
实际上传递给函数的是一个引用。命名参数
b
成为参数对象q
的同义词。编译器可能如何实现这一点:调用者在调用之前将
q
的地址放置在堆栈或寄存器中,并且被调用者使用该值来实现对b
的所有访问。但将其描述为“实际传递”指针可能会产生误导,因为参数传递是 C++ 语言级别的概念,并且在该级别上它与传递指针不是同一概念。例如,当您传递指针时,您可以传递空指针,但当您传递引用时,您不能(有效)。所以说它们是同一件事是错误的。也就是说,实现编译器的人可能会将其描述为“实际上传递一个指针”,并且您知道他们的意思。作为比较,如果 char 变量在调用约定中占用 4 字节堆栈槽,则它们可能会说编译器“实际上正在传递 int”。所以这取决于“实际上”的含义。
What's actually passed to the function is a reference. The named parameter
b
becomes a synonym for the argument objectq
.How the compiler probably implements this that the caller places the address of
q
on the stack or in a register before calling, and the callee uses that value to effect all accesses tob
. But it could be misleading to describe that as "actually passing" a pointer, because parameter passing is a concept at the level of the C++ language, and at that level it is not the same concept as passing a pointer. For instance, when you pass a pointer you can pass a null pointer, but when you pass a reference you cannot (validly). So it'd be wrong to say they're same thing.That said, the person implementing the compiler might describe it as "actually passing a pointer", and you know what they mean. For comparison, if
char
variables occupy 4-byte stack slots in the calling convention, they might say that the compiler is "actually passing an int". So it depends what "actually" is supposed to mean.其他答案提到了引用和指针之间的语义差异。
实际上,我使用过的每个编译器都以相同的方式实现它们——传递引用实际上是在汇编级别传递指针。这没有在任何标准中指定,只是各地实践中的情况。
这个问题之前就出现过: 指针和引用之间的低级区别是什么?
The other answers mention the semantic difference between a reference and a pointer.
In practice, every single compiler I've ever worked with implements them the same way -- passing a reference is really passing a pointer at the assembly level. This isn't specified in any standard, it's just the case in practice everywhere.
The question's come up on SO before: What's the low-level difference between a pointer an a reference?
它确实传递了一个引用类型——它有点像地址,但又不完全一样。实际地址将是一个指针。引用不如指针强大,但可以说更安全。 维基百科对指针和指针之间的差异有很好的描述参考。
It really gets passed a reference type - it's sort of like the address, but not quite. The actual address would be a pointer. References are less powerful than pointers, but are arguably safer. Wikipedia has a good description of the differences between pointers and references.
您传递的是一个引用,它不是指针,也不是地址——但它是相似的。
“确切”的参考是什么,并不是一成不变的。该标准没有规定处理引用的机制——只是规定使用它们的后果。 通常,它们将被实现为指针。
示例:
这可能会生成与以下内容相同的机器代码:
但不能保证这一点,并且两者并不等效(尽管它们提供类似的功能)。
当您获取引用的地址时,您将获得与其所引用的对象相同的地址。例子:
You are passing a reference, which is not a pointer, and not an address -- but it is similar.
What "exactly" a reference is, isn't set in stone. The standard does not dictate the mechanisms for dealing with reference -- just the consequences of using them. Usually, they will be implemented as pointers.
Example:
This might generate the same machine code as:
But there's no guarantee of that, and the two are NOT equivalent (although they provide similar functionality).
When you take the address of a reference, you get the same address as the object that it is referring to. Example:
引用传递的工作方式由编译器定义。无论如何,它们都作为
int&
传递,这是 C++ 中的实际类型。试试这个:你认为 y 的值是多少?引用并不完全是指针,而是变量的别名。编译器应用于
int&
的机制也用于按引用传递参数。给定以下程序:
为
byRef
和byPtr
调用生成的 MSVC90 程序集完全相同,即:The way pass by reference works is defined by your compiler. In any event, they are passed as
int&
which is an actual type in C++. Try this:What do you think is the value of y? References are not quite pointers but aliases to a variable. The same mechanism that your compiler applies to
int&
is used for pass by reference parameters.Given the following program:
The MSVC90 Assembly generated for the
byRef
and forbyPtr
calls are exactly the same which is: