为什么 C 中的参数通过引用传递?不需要解引用运算符?
我是 C++ 社区的新手,只是有一个关于 C++ 如何通过引用函数传递变量的快速问题。
当您想要在 C++ 中通过引用传递变量时,可以将 &
添加到您想要通过引用传递的任何参数中。当您为通过引用传递的变量赋值时,为什么要说 variable = value;
而不是 *variable = value
?
void add_five_to_variable(int &value) {
// If passing by reference uses pointers,
// then why wouldn't you say *value += 5?
// Or does C++ do some behind the scene stuff here?
value += 5;
}
int main() {
int i = 1;
add_five_to_variable(i);
cout << i << endl; // i = 6
return 0;
}
如果 C++ 使用指针通过幕后魔法来完成此操作,为什么不需要像指针那样取消引用?任何见解将不胜感激。
I'm new to the C++ community, and just have a quick question about how C++ passes variables by reference to functions.
When you want to pass a variable by reference in C++, you add an &
to whatever argument you want to pass by reference. How come when you assign a value to a variable that is being passed by reference why do you say variable = value;
instead of saying *variable = value
?
void add_five_to_variable(int &value) {
// If passing by reference uses pointers,
// then why wouldn't you say *value += 5?
// Or does C++ do some behind the scene stuff here?
value += 5;
}
int main() {
int i = 1;
add_five_to_variable(i);
cout << i << endl; // i = 6
return 0;
}
If C++ is using pointers to do this with behind the scenes magic, why aren't dereferences needed like with pointers? Any insight would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您编写时,
这是将 3 分配给指针
p
引用的对象的语法。当您编写时,这是将 3 分配给引用
r
引用的对象的语法。语法和实现不同。引用是使用指针实现的(除非它们被优化),但语法不同。因此,您可以说解引用在需要时自动发生。
When you write,
That is syntax for assigning 3 to the object referred to by the pointer
p
. When you write,That is syntax for assigning 3 to the object referred to by the reference
r
. The syntax and the implementation are different. References are implemented using pointers (except when they're optimized out), but the syntax is different.So you could say that the dereferencing happens automatically, when needed.
C++ 在幕后使用指针,但向您隐藏了所有这些复杂性。通过引用传递还可以使您避免与无效指针相关的所有问题。
C++ uses pointers behind the scenes but hides all that complication from you. Passing by reference also enables you to avoid all the problems asssoicated with invalid pointers.
当您通过引用将对象传递给函数时,您可以直接在函数中操作该对象,而无需像指针那样引用其地址。因此,在操作此变量时,您不希望使用
*variable
语法取消引用它。这是通过引用传递对象的好习惯,因为:器如何实现“按引用传递”与您的情况并不真正相关。
维基百科中的文章是一个很好的资源。
When you pass an object to a function by reference, you manipulate the object directly in the function, without referring to its address like with pointers. Thus, when manipulating this variable, you don't want to dereference it with the
*variable
syntax. This is good practice to pass objects by reference because:How the compiler achieves the "pass by reference" is not really relevant in your case.
The article in Wikipedia is a good ressource.
似乎有两个问题:
让我们分别解决这两个问题。
引用和指针的语法
从概念上讲,指针是指向对象的“标志”(如路标)。它允许两种操作:
operator*
和operator->
允许您访问指针对象,以将其与对指针本身的访问区分开来。引用不是“符号”,而是别名。在它的生命周期内,无论是地狱还是高潮,它都会指向同一个对象,你对此无能为力。因此,由于您无法访问引用本身,因此没有必要使用奇怪的语法
*
或->
来打扰您。讽刺的是,不使用奇怪的语法被称为语法糖。引用的机制
C++ 标准没有提及引用的实现,它只是暗示如果编译器可以的话,就可以删除它们。例如,在下面的情况下:
一个好的编译器会意识到
b
只是a
的代理,没有任何怀疑的余地,因此直接访问a
并优化b
。正如您所猜测的,引用的可能表示形式是(在底层)指针,但不要让它打扰您,它不会影响语法或语义。然而,这确实意味着指针的许多问题(例如访问已删除的对象)也会影响引用。
There are two questions in one, it seems:
Let's address the two separately.
Syntax of references and pointers
A pointer is, conceptually, a "sign" (as road sign) toward an object. It allows 2 kind of actions:
The
operator*
andoperator->
allow you to access the pointee, to differenciate it from your accesses to the pointer itself.A reference is not a "sign", it's an alias. For the duration of its life, come hell or high water, it will point to the same object, nothing you can do about it. Therefore, since you cannot access the reference itself, there is no point it bothering you with weird syntax
*
or->
. Ironically, not using weird syntax is called syntactic sugar.Mechanics of a reference
The C++ Standard is silent on the implementation of references, it merely hints that if the compiler can it is allowed to remove them. For example, in the following case:
A good compiler will realize that
b
is just a proxy fora
, no room for doubts, and thus simply directly accessa
and optimizeb
out.As you guessed, a likely representation of a reference is (under the hood) a pointer, but do not let it bother you, it does not affect the syntax or semantics. It does mean however that a number of woes of pointers (like access to objects that have been deleted for example) also affect references.
设计不需要显式取消引用 - 这是为了方便。当您在引用上使用
.
时,编译器会发出访问实际对象所需的代码 - 这通常包括取消引用指针,但无需在代码中显式取消引用即可完成此操作。The explicit dereference is not required by design - that's for convenience. When you use
.
on a reference the compiler emits code necessary to access the real object - this will often include dereferencing a pointer, but that's done without requiring an explicit dereference in your code.