简体   繁体   English

当我们将引用分配给变量时会发生什么?

[英]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;如果你在谈论dc ,我假设你指的是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成为aint 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.

相关问题 将指针指定为NULL时会发生什么? - What will happen when we assign a pointer NULL? 当我声明一个引用变量时,堆栈会发生什么? C ++ - What happen to stack when i declare a reference variable? C++ 当我们使用VS在C ++项目中将库作为附加依赖项引用时,究竟发生了什么? - What exactly happen when we reference a library as additional dependency in a C++ project using VS? 我们可以将 integer 值分配给参考变量吗? - can we assign a integer value to a reference variable? 当我们在C ++中将常量整数引用分配给整数指针时,行为是什么? - What is the behaviour when we assign constant integer reference to a integer pointer in c++? 当我给char分配一个short时会发生什么类型的转换? - What type conversion happen when I assign a short to a char? 当我们在派生的 class 中编写空的复制构造函数时会发生什么? - What happen when we write empty copy constructor in derived class? 当一个class继承了一个引用私有属性的公有方法时会发生什么? - What will happen when a class inherits a public method with reference to private properties? 将左值分配给右值引用时会发生什么? 没有破坏临时对象? - What happen when a lvalue assigned to a rvalue reference? No destruction of the temporary object? 如果我们声明一个对象并将其分配给同一类的另一个对象,将会发生什么 - What will happen if we declare one object and assign it to another object of same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM