赋值运算符的行为必须与复制构造函数完全相同 [英] Does assignment operator have to behave exactly like copy constructor

查看:59
本文介绍了赋值运算符的行为必须与复制构造函数完全相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望Matrix A(B)创建B的浅拷贝但是A = B来创建B的深层

副本。这是不好的设计?为什么不这样做?

I want Matrix A(B) to create shallow copy of B but A=B to create deep
copy of B. Is that bad design? Why and why not?

推荐答案



< bl ********* *@gmail.com>在消息中写道

news:11 ********************** @ g44g2000cwa.googlegr oups.com ...

<bl**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
我希望Matrix A(B)创建B的浅拷贝,但是A = B来创建B的深层副本。这是不好的设计?为什么不呢?
I want Matrix A(B) to create shallow copy of B but A=B to create deep
copy of B. Is that bad design? Why and why not?




我想这取决于你为什么这样做,以及你的Matrix

对象包含什么。在两者之间有不同的行为并不是b b b b b b b b b b b b b b b b b b b b b糟糕的设计,但它*可能*。


如果通过浅拷贝你的意思是你的Matrix类包含指针或

引用,或者它们自己拥有动态分配数据的成员,并且你的复制构造函数只复制指针或引用,然后是可以

肯定会导致你的问题。当你去删除其中一个对象时,

剩余对象将有一个指针或对已删除数据的引用。但是

这是复制构造函数本身的问题,而不是分配

运算符不同。


没有看到你的设计,我们不能确定它是否错了。

问题是,为什么要让它们与众不同?并且一定要了解

每个函数的调用时间。 (例如,你知道

MyClass x = y;是初始化,而不是作业,对吗?


-Howard



I guess it depends on why you''re doing it that way, and what your Matrix
object contains. Having different behavior between the two isn''t
"inherently" bad design, but it *could* be.

If by "shallow copy" you mean that your Matrix class contains pointers or
references, or members which themselves have dynamically-allocated data, and
your copy constructor only copies the pointer or reference, then that can
certainly cause you problems. When you go to delete one of the objects, the
remaining object will then have a pointer or reference to deleted data. But
this is a problem with the copy constructor itself, not with the assignment
operator being different.

Without seeing your design, we can''t say for sure if it''s wrong or not. The
question is, WHY do want them different? And be sure you understand when
each of those functions wil be called. (For instance, you know that
"MyClass x = y;" is an initialization, not an assignment, right?

-Howard


bl ********** @ gmail .com 写道:
我希望Matrix A(B)创建B的浅拷贝但是A = B来创建B的深层副本。这是不好的设计?为什么为什么不呢?
I want Matrix A(B) to create shallow copy of B but A=B to create deep
copy of B. Is that bad design? Why and why not?




要确定某些决定(包括设计决策)是否错误或者b
好​​,需要知道背后的动机你为什么认为你需要他们表现得与众不同?


副本构建或复制作业都有复制作为他们的密钥

字。你实现它们。所以,你必须有一个令人信服的理由

才能使它们与众不同,否则,如果你问一个非专业人士,他们会

只是说,嘿,你正在制作一份副本,对吗?所以,它笑uld

是同一件事。


如果您的目标是实施懒惰复制或写时复制有点像b $ b的东西,那么两者都应该基本上这样做,而且你需要使用一些

的引用计数变量。如果这不是你的目标,那么呢?


V



To determine whether some decision (design decision included) is bad or
good, one needs to know the motivation behind the decision. Why do you
think you need them to behave differently?

The copy-construction or copy-assignment have the "copy" as their key
word. You implement them both. So, you must have a compelling reason
to make them different, otherwise, if you asked a lay person, they''d
just say, "Hey, you''re making a copy either way, right? So, it should
be the same thing."

If your goal is to implement "lazy copying" or "copy-on-write" sort of
thing, then both should essentially do it, and you need to employ some
variation of reference counting. If that''s not your goal, then what?

V


bl ********** @ gmail.com 写道:
我想要Matrix A(B)创建B的浅拷贝,但是A = B来创建B的深层副本。这是不好的设计?为什么和为什么不呢?
I want Matrix A(B) to create shallow copy of B but A=B to create deep
copy of B. Is that bad design? Why and why not?




1.这是糟糕的设计,因为它违反了最少惊喜的规则。

2.你不要不需要它:

Matrix& A = B;



1. It''s bad design because it violates the rule of least surprise.
2. You don''t need it:
Matrix& A = B;


这篇关于赋值运算符的行为必须与复制构造函数完全相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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