默认赋值运算符 [英] default assignment operator

查看:111
本文介绍了默认赋值运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将对象分配给另一个时,

由编译器编写的默认赋值运算符

使成员复制为什么没有任何问题

类型如(int,double等),但它不能复制字符串,这些都没问题。

我不明白它是如何管理分配数组的。

let我用一个例子来解释:

展开 | 选择 | Wrap | 行号

解决方案

这是有效的,因为每个数组中的数组class只是变量。但在这种情况下,像这样复制它们并不是一个好主意。实际存储在A.array和B.array中的值是指针 - 内存中实际存储5个整数的地址。当你输入A.array = B.array时,你给A.array提供了与B.array相同的内存地址。现在A.array和B.array指向相同的记忆。


但是当B超出范围时会发生什么?它被解除分配,并且B.array指向的内存被删除。但A.array仍然指向同一个地方,现在已经没用了!


我似乎无法成功发布你应该如何做到这一点;愚蠢的学校电脑。也许我的一位同事可以完成我的解释。



这是有效的,因为每个类中的数组只是变量。但在这种情况下,像这样复制它们并不是一个好主意。实际存储在A.array和B.array中的值是指针 - 内存中实际存储5个整数的地址。当你输入A.array = B.array时,你给A.array提供了与B.array相同的内存地址。现在A.array和B.array指向相同的记忆。


但是当B超出范围时会发生什么?它被解除分配,并且B.array指向的内存被删除。但A.array仍然指向同一个地方,现在已经没用了!


我似乎无法成功发布你应该如何做到这一点;愚蠢的学校电脑。也许我的一位同事可以完成我的解释。



对不起Ganon,不太对劲。该数组不是单独存储的,而是一个聚合项(C / C ++术语,UML将其称为复合项.Grrr,刚刚发现UML使用了来自C / C ++的不同术语:(),因此整个数组复制。


如果它是一个指向数组的指针(UML可能会将其声明为聚合关联,尽管你可以从主对象中物理地分离出一些东西但仍然有一个组合关联)你是正确的,因为只有指针会被处理,就像OP在他/她的例子中所说的那样。在这种情况下,它会在进行内存清理时引起问题。如果没有正确完成,你可以有一个指向另一个不存在的对象(已被其他拥有对象删除)。


Adrian


因为数组是用大括号[]声明的,它会被正确复制吗?但是如果数组已经被声明为指针,那么我所说的将是真的,对吗?

while assigning on object to another,
default assignment operator written by compiler
makes memberwise copying that why there isn''t any problem with
types like (int ,double etc) but it cannot copy strings, these are ok.
what i dont understand is how it can manage assigning arrays.
let me explain with an example:

Expand|Select|Wrap|Line Numbers

解决方案

This works because the arrays in each class are just variables. In this case, though, it''s not a good idea to copy them like this. The value actually being stored in A.array and B.array are pointers - addresses in memory where the 5 integers will actually be stored. When you type A.array = B.array, you are giving A.array the same memory address that B.array was holding. Now A.array and B.array are pointing to the same memory.

But what happens when, say, B goes out of scope? It is de-allocated, and the memory pointed to by B.array is erased. But A.array is still pointing to that same place, and is now useless!

I can''t seem to successfully post how you should go about this; stupid school computer. Perhaps one of my colleagues can finish my explanation.


This works because the arrays in each class are just variables. In this case, though, it''s not a good idea to copy them like this. The value actually being stored in A.array and B.array are pointers - addresses in memory where the 5 integers will actually be stored. When you type A.array = B.array, you are giving A.array the same memory address that B.array was holding. Now A.array and B.array are pointing to the same memory.

But what happens when, say, B goes out of scope? It is de-allocated, and the memory pointed to by B.array is erased. But A.array is still pointing to that same place, and is now useless!

I can''t seem to successfully post how you should go about this; stupid school computer. Perhaps one of my colleagues can finish my explanation.

Sorry Ganon, not quite right. The array is not stored on the seperatly, but is an aggregate item (C/C++ term, UML states this as a composite item. Grrr, just found out that UML uses different terms from C/C++ :(), so the entire array is copied.

If it were a pointer to an array (UML may state this as a aggregate association, though you can have something that is physicly seperated from the main object and still have a composition association) you would be correct in that only the pointer would be coped, just like the c-string that OP stated in his/her example. In which case, it would cause a problem when doing memory cleanup. If not done properly, you could have an object pointing at another that is not there (has been deleted by the other owning object).


Adrian


So because the array was declared with the braces [], it will be copied correctly? But if the array had been declared as a pointer, then what I said would hold true, right?


这篇关于默认赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆