>> No, it is not shallow copying. It is in fact, like you said, two
>> references to *the same* object. Shallow copying in C++ means
[quoted text clipped - 3 lines]
> also describe deep copying. In fact the issue of shallow copying is
> that you end up with two references/pointers to *the same* object.

Signature
Beware the False Authority Syndrome
> >> No, it is not shallow copying. It is in fact, like you said, two
> >> references to *the same* object. Shallow copying in C++ means
[quoted text clipped - 7 lines]
> that after a shallow copy you have two references to different (yes
> different, not the same, albeit identical) objects.
Yes this is true, but the same is also true for a deep copy.
> If the object is a
> compound object then the references inside both objects will be to the
> same object/memory space.
Yes and it is this that is the important point that distinguishes a
shallow from a deep copy.
> But the original object and its copy are not
> the same.
[quoted text clipped - 12 lines]
> but not for compound objects. Hence the whole problem with shallow
> copying: people not defining a copy constructor for compound objects.
Sure, but in Java any object which contains another object is a
compound object. Most of time in c++ this is not the case and the
default copy constructor works fine, even where member objects will
need to perform a deep copy, example:
class SomeObj
{
std::vector<SomeCompoundObj> array_;
//Some other functions, but no user defined copy constructor.
}
SomeObj Obj1;
SomeObj Obj2(Obj1);
In the above the default copy constructor works fine because vector
performs a deep copy as does each SomeCompoundObj in the array.
> This problem does not exist in Java because there is no default copy
> constructor and clone is protected. If you want a copy of an object you
> need to create a copy constructor or implement clone, both of which
> protect you from forgetting to implement the copy constructor, as often
> happens in C++.
Forgetting to implement the copy constructor sounds like a very poorly
designed class to me. If they forgot to implement the copy constructor
(or to disable it completely) it is most probably not the only thing
they forgot. Of course, both cloning and copy constructors in Java have
their own gotchas.
> Of course it doesn't protect you from writing bad copy
> code.
Sure, you can never protect a bad programmer from themselves.
Another important difference is that Java doesn't have implicit
copying, however, it is easier to correctly implement copying or
cloning in c++ than Java (IMHO).
zero - 23 Dec 2005 18:17 GMT
"AndyRB" <xandyrb@hotmail.com> wrote in news:1135342995.115424.168520
@g44g2000cwa.googlegroups.com:
> Another important difference is that Java doesn't have implicit
> copying, however, it is easier to correctly implement copying or
> cloning in c++ than Java (IMHO).
Why do you think that? The only part of copying that I think is easier in
C++ than in Java is that in C++ the * syntax clearly shows when you're
working with pointers. The lack of a specific syntax in Java can make you
forget that every class-type variable is a reference - especially if you
come from C++.

Signature
Beware the False Authority Syndrome
AndyRB - 23 Dec 2005 22:11 GMT
> "AndyRB" <xandyrb@hotmail.com> wrote in news:1135342995.115424.168520
> @g44g2000cwa.googlegroups.com:
[quoted text clipped - 8 lines]
> forget that every class-type variable is a reference - especially if you
> come from C++.
To be honest, thinking about it more, I am not sure there is much of a
difference.
But my original thoughts came from the fact that clone is often
criticised in Java, whereas in C++ once you have the copy constructor,
clone can then simply be implemented in terms of the copy constructor:-
virtual Dervied * Clone() { return new Derived(*this); }
And if you need to avoid any slicing issues, you just declare the copy
constructor as protected and therefore as an implementation mechanism
only.
Maybe a more significant factor is that the default copy (as in basic
memberwise copy not clone) is probably more often the appriopriate copy
to perform in C++ than in Java because every class-type variable is a
reference in Java, whereas often you have the class-value itself as the
data member in C++.