简体   繁体   English

将传递引用变量分配给struct成员时会发生什么

[英]What happens when assigning passed-by-reference variable to struct member

I defined this struct : 我定义了这个struct

template<class T>
struct Node {
   T value;
   Node(const T& v) : value(v) {}
};

I want to know what exactly happens when I do 我想知道我做什么时会发生什么

int i = 3;
Node<int>* my_node = new Node<int>(i);

By printing out the address of i and the address my_node->value I realized that they don't refer to the same address. 通过打印出i的地址和my_node->value的地址,我意识到它们没有引用相同的地址。 I did expect this behavior because it doesn't make sense to force my new pointer to be placed at a specific address, to be able to wrap my passed-by-reference argument!!! 我确实期望这种行为,因为强行将我的新指针置于特定地址并能够包装我的按引用传递参数没有意义!!!

But I'm wondering what's the role of & ? 但是我想知道&的作用是什么? Is it just to make sure argument is not copied when passed to the method? 只是为了确保传递给方法时不会复制参数吗? And if yes, is that just to avoid heavy copying? 如果是,那是否只是为了避免大量复制? Or is there another purpose for using & ? 还是使用&还有其他目的?

And value(v) or value = v simply copy the whole datatype into a new memory space, no matter if v is passed by reference or not, am I right? 而且value(v)value = v只是将整个数据类型复制到一个新的内存空间中,无论v是否通过引用传递,对吗?

Edit: I'm sorry I tried to shorten my code and ended up making it ambiguous. 编辑:对不起,我试图缩短代码长度并最终使其模棱两可。 Just fixed it. 修复它。

This statement is badly formed: Node* my_node = new Node(i); 该语句的格式不正确:Node * my_node = new Node(i);

But I'm wondering what's the role of &? 但是我想知道&的作用是什么? Is it just to make sure argument is not copied when passed to the method? 只是为了确保传递给方法时不会复制参数吗? And if yes, is that just to avoid heavy copying (in case of a big datatype other than int in this example)? 如果是,那是否只是为了避免大量复制(在本示例中为int以外的大数据类型的情况下)? Or is there another purpose for using &? 还是使用&还有其他目的?

The role of & in this case is exactly as you have said. 在这种情况下,&的作用与您所说的完全相同。 The general rule of thumb that I use, is to pass built-ins (eg int, double) by value and pass everything else by const reference to avoid unnecessary copying. 我使用的一般经验法则是按值传递内置函数(例如int,double),并按const引用传递其他所有东西,以避免不必要的复制。

And value(v) or value = v simply copy the whole datatype into a new memory space, no matter if v is passed by reference or not, am I right? 而且value(v)或value = v只是将整个数据类型复制到一个新的内存空间中,无论v是否通过引用传递,对吗?

That is correct also. 那也是正确的。 But for non-built-ins, these two statements differ when used in a constructor. 但是对于非内置程序,这两个语句在构造函数中使用时有所不同。 The first is initialization and is done in the initialization list (which you have done in your example), the latter is assignment and is done in the constructor body. 第一个是初始化,并在初始化列表中完成(在示例中已完成),第二个是赋值,并在构造函数主体中完成。 For non-built-ins, it is generally more efficient to use the initialization list. 对于非内置插件,使用初始化列表通常会更有效。 Otherwise, the object is default initialized before you do the assignment. 否则,将在执行分配之前默认初始化对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 变量按引用传递在 function scope 中更改,但在主 scope 中未更改 - Variable passed-by-reference is changed in function scope but not in main scope 从C ++的构造函数中向成员const char *变量分配字符串文字时会发生什么? - What happens when assigning a string literal to a member const char* variable from within a constructor in C++? 分配通过引用传递给成员变量的值(在C ++中) - Assigning value passed by reference to a member variable (in C++) 如果我得到了一个与符号一起传递的变量的引用,将会发生什么? - What happens if I get the reference of a variable passed with an ampersand? 将右值引用分配给成员变量 - Assigning rvalue reference to member variable 当我返回传递给函数的引用参数时,内部会发生什么? - What happens internally when I return a reference parameter passed to a function? C ++为什么此按引用传递数组会生成运行时错误? - C++ Why is this passed-by-reference array generating a runtime error? 为什么int在函数中不需要是常量和/或按引用传递? - Why does int do not require to be a constant and/or passed-by-reference in functions? 当参考变量“死亡”时,该变量会发生什么? - What happens to a reference variable when its reference 'dies'? C ++从传递的参考变量分配参考变量? - C++ Assigning reference variable from passed in reference variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM