[英]What happen when we assign a reference to a variable?
void func(){
int a = 1;
int b = a; // copy assignemnt
int &c = a; // nothing?
int d = c; // nothing?
}
For example, when we assign reference c
to d
, is there anything triggered (move, copy, etc.)?例如,当我们将引用
c
分配给d
时,是否有任何触发(移动、复制等)?
What if d
is an instance member?如果
d
是实例成员怎么办? Is it safe to store a local variable reference into it?将局部变量引用存储到其中是否安全?
when we assign reference c to d, is there anything triggered (move, copy, etc.)?
当我们将引用 c 分配给 d 时,是否有任何触发(移动、复制等)?
Your terminology's getting me confused!你的术语让我感到困惑! If you're talking about
d
and c
, I assume you're referring to int d = c;
如果你在谈论
d
和c
,我假设你指的是int d = c;
I'd describe that as "assigning whatever c
refers to to d
".我将其描述为“将
c
指的任何内容分配给d
”。 "Assign reference c to d" sounds too much like int& c = d;
“将引用 c 分配给 d”听起来太像
int& c = d;
. .
Your code has:你的代码有:
int &c = a; // nothing?
int d = c; // nothing?
In the first line, nothing happens that you need to care about... c
can be thought of as a nickname or alias for a
.在第一行中,没有发生任何你需要关心的事情……
c
可以被认为是a
昵称或别名。 (At a hidden implementation level, it may or may not help to image c
being an automatically-dereferenced pointer variable, and your int &c = a;
being similar to int* p_c = &a;
- it doesn't copy or move any int
values around, nor change a
in any way.) (在隐藏的实现级别,它可能会或可能不会帮助图像
c
成为一个自动取消引用的指针变量,并且您的int &c = a;
类似于int* p_c = &a;
- 它不会复制或移动任何int
周围的值,也不会以任何方式改变a
。)
In the second line, the variable d
is assigned a copy of the value from a
.在第二行中,变量
d
被分配了 a 值a
副本。
What if d is an instance member?
如果 d 是实例成员怎么办? Is it safe to store a local variable reference into it?
将局部变量引用存储到其中是否安全?
You're thinking of this the wrong way.你在想这个错误的方式。 When you assign to a member reference, you're assigning to the referenced object, and not the reference itself.
当您分配给成员引用时,您分配给引用的 object,而不是引用本身。 The referenced object will be another
int
somewhere, and assignment from one int
to another works as usual.引用的 object 将是某个地方的另一个
int
,并且从一个int
到另一个 int 的分配照常进行。
int &c = a;
c
now refers to the same memory as the variable a
. c
现在指的是与变量a
相同的 memory。 It doesn't matter where a
is instantiated. a
在何处实例化并不重要。 When followed by当紧随其后
int d = c;
d
gets the value in the memory referred to by c
, that is, the same memory as a
, Which contains a 1
. d
获取 c 引用的c
中的值,即与a
相同的 memory ,其中包含一个1
。 So now d==1
.所以现在
d==1
。
Since d
is not a reference, it has its own memory (regardless of where that is) and its own value.由于
d
不是引用,它有自己的 memory(不管它在哪里)和自己的价值。
Of course, if you were instead to say当然,如果你改为说
c = 2;
then the memory referenced by c
(that is, the same memory as a
) is assigned the value 2
.然后 c 引用的
c
(即与a
相同的 memory)被赋予值2
。 So now a==2
.所以现在
a==2
。 It also doesn't matter where a
is declared;在哪里声明
a
也无关紧要; it's still assigned to 2
.它仍然分配给
2
。
A reference creates an alias to another variable.引用创建另一个变量的别名。
int &c= a;
makes c
an alias of a
and int d= c;
使
c
成为a
和int d= c;
has the same effect as d= a;
与
d= a;
A reference is safe, as its lifetime is shorter than that of the variable it points to.引用是安全的,因为它的生命周期比它指向的变量的生命周期短。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.