B
bluekite2000
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?
copy of B. Is that bad design? Why and why not?
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 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 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 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?
Panjandrum said:1. It's bad design because it violates the rule of least surprise.
2. You don't need it:
Matrix& A = B;
Andre said:(e-mail address removed) wrote in @g44g2000cwa.googlegroups.com:
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...
Howard said: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).
Panjandrum said:And what is a 'shallow copy' supposed to do?
Howard said:I believe a "shallow copy" of an object means that the object's contents are
copied bit-wise. In that case, member pointers and references end up
pointing at the same addresses in both the original and the newly created
object.
A "deep copy" would be a member-wise copy, so that dynamic members would
also be copy-constructed. The pointers and references in the new object
would thus point to different addresses than the original's.
Panjandrum said:not the 'usual' definition, see e.g.
http://en.wikipedia.org/wiki/Shallow_copy
agreed
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.