Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Does assignment operator have to behave exactly like copy constructor

Reply
Thread Tools

Does assignment operator have to behave exactly like copy constructor

 
 
bluekite2000@gmail.com
Guest
Posts: n/a
 
      06-16-2005
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?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-16-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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?


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
 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      06-16-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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?
>


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


 
Reply With Quote
 
Panjandrum
Guest
Posts: n/a
 
      06-16-2005
(E-Mail Removed) wrote:
> 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. It's bad design because it violates the rule of least surprise.
2. You don't need it:
Matrix& A = B;

 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      06-16-2005
(E-Mail Removed) wrote in news:1118937876.146898.155840
@g44g2000cwa.googlegroups.com:

> 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?


Highly unadvisable.

Principle of least surprise. What would the two code snippets do
(assuming the appropriate operator definitions, and that a and b can be
added together... it's been a long time since I've had to do linear
algebra...):

{
Matrix a, b;

// Fill a and b with something reasonable

Matrix c(a); // Shallow copy

c += b;
}

vs.

{
Matrix a, b;

// Fill a and b with something reasonable

Matrix c;

c = a; // Deep copy
c += b;
}


What would the contents of a, b, and c be in both cases?
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      06-16-2005

"Panjandrum" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> (E-Mail Removed) wrote:
>> 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. It's bad design because it violates the rule of least surprise.


I think you're making that one up!

> 2. You don't need it:
> Matrix& A = B;
>


How does that help? It's not a copy at all, but a reference to an existing
object. Any changes you then make to the members of A also happen to the
members of B (and vise-versa).

-Howard


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-16-2005
Andre Kostur wrote:
> (E-Mail Removed) wrote in news:1118937876.146898.155840
> @g44g2000cwa.googlegroups.com:
>
>
>>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?

>
>
> Highly unadvisable.
>
> Principle of least surprise. What would the two code snippets do
> (assuming the appropriate operator definitions, and that a and b can be
> added together... it's been a long time since I've had to do linear
> algebra...):
>
> {
> Matrix a, b;
>
> // Fill a and b with something reasonable
>
> Matrix c(a); // Shallow copy
>
> c += b;
> }
>
> vs.
>
> {
> Matrix a, b;
>
> // Fill a and b with something reasonable
>
> Matrix c;
>
> c = a; // Deep copy
> c += b;
> }
>
>
> What would the contents of a, b, and c be in both cases?


The same because += actually makes a deeper copy behind the scenes. It's
called "copy-on-write". Of course, a simple assignment could be made to
perform shallow copy just as well in that case...

(It's a speculation, of course. The OP should answer this question,
however, it basically goes to the argument that you cannot answer the
original question without knowing more about the Matrix design and its
intentions)

V
 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      06-16-2005
Victor Bazarov <(E-Mail Removed)> wrote in news:qaise.3091
$(E-Mail Removed)01.us.to.verio.net:

> Andre Kostur wrote:
>> (E-Mail Removed) wrote in news:1118937876.146898.155840
>> @g44g2000cwa.googlegroups.com:
>>
>>
>>>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?

>>
>>
>> Highly unadvisable.
>>
>> Principle of least surprise. What would the two code snippets do
>> (assuming the appropriate operator definitions, and that a and b can

be
>> added together... it's been a long time since I've had to do linear
>> algebra...):
>>
>> {
>> Matrix a, b;
>>
>> // Fill a and b with something reasonable
>>
>> Matrix c(a); // Shallow copy
>>
>> c += b;
>> }
>>
>> vs.
>>
>> {
>> Matrix a, b;
>>
>> // Fill a and b with something reasonable
>>
>> Matrix c;
>>
>> c = a; // Deep copy
>> c += b;
>> }
>>
>>
>> What would the contents of a, b, and c be in both cases?

>
> The same because += actually makes a deeper copy behind the scenes.

It's
> called "copy-on-write". Of course, a simple assignment could be made

to
> perform shallow copy just as well in that case...


I'm assuming no COW in play here since the OP has two different semantics
for copy construction and assignment (suggesting that the OP isn't aware
of all of the implications behind that decision).... as you mentioned, if
COW was in play, there's no point in doing a deep copy during the
assignment.
 
Reply With Quote
 
bluekite2000@gmail.com
Guest
Posts: n/a
 
      06-16-2005
Say I have M=1 3
2 4
V=0
0
Vector Vcol=M.Col(1) //shallow copying
M.Col(1) =V //shallow copying
cout<<M<<endl;
1 3
2 4
cout<<Vcol<<endl;
3
4

But I want M to = 1 0
2 0

So I changed my assignment operator to do deep copying, thus solving
the problem.

 
Reply With Quote
 
Panjandrum
Guest
Posts: n/a
 
      06-16-2005
Howard wrote:
> "Panjandrum" <(E-Mail Removed)> wrote in message
> > 2. You don't need it:
> > Matrix& A = B;
> >

>
> How does that help? It's not a copy at all, but a reference to an existing
> object. Any changes you then make to the members of A also happen to the
> members of B (and vise-versa).


And what is a 'shallow copy' supposed to do?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Copy Constructor and overloaded assignment operator with templates saxman C++ 2 06-12-2007 02:49 PM
Copy constructor and assignment operator inheritance Henrik Goldman C++ 2 12-26-2006 05:07 PM
Benefits of copy constructor over assignment operator Ajay C++ 4 05-12-2006 04:30 AM
copy constructor, assignment operator in template Martin Vorbrodt C++ 3 09-09-2005 06:02 PM
Copy Constructor & assignment operator ambar.shome@gmail.com C++ 10 07-05-2005 05:38 AM



Advertisments