Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > deep/shallow copy - constructor v Object.copy()

Reply
Thread Tools

deep/shallow copy - constructor v Object.copy()

 
 
VisionSet
Guest
Posts: n/a
 
      04-25-2004
Is there any kind of convention that dictates where it is usual to perform
shallow and deep copies?
I prefer to use copy constructors to implement any kind of copy, but now I
find the need for a shallow and deep copy in the same class.
I haven't ever needed to implement copy(), I imagine that its purpose is for
RTTI.

--
Mike W


 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      04-25-2004
"VisionSet" <(E-Mail Removed)> wrote in message
news:s3Uic.166$cy3.127@newsfe1-win...
> Is there any kind of convention that dictates where it is usual to perform
> shallow and deep copies?
> I prefer to use copy constructors to implement any kind of copy, but now I
> find the need for a shallow and deep copy in the same class.
> I haven't ever needed to implement copy(), I imagine that its purpose is

for
> RTTI.
>


And where I said copy() of course I meant clone().

--
Mike W


 
Reply With Quote
 
 
 
 
Karthik
Guest
Posts: n/a
 
      04-25-2004
VisionSet wrote:
> Is there any kind of convention that dictates where it is usual to perform
> shallow and deep copies?
> I prefer to use copy constructors to implement any kind of copy, but now I
> find the need for a shallow and deep copy in the same class.


Can you elaborate on this a little bit. When you say, you want both
shallow and deep copy, you would want to revisit that.
Most of the cases, people override the function only because they
want a deep copy.
If there is still a valid reason, why there ought to be two copies, I
would suggest that - you write a method, something like this.

public Object shallowCopy( Object o ) {
..........
}

But think twice if you really need something like that ( that would
affect the 'readability' of the code for sure ).

--
Karthik
------
Human Beings please 'removeme' for my email.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-25-2004
On Sun, 25 Apr 2004 20:11:02 +0100, "VisionSet" <(E-Mail Removed)>
wrote or quoted :

>I prefer to use copy constructors to implement any kind of copy, but now I
>find the need for a shallow and deep copy in the same class.
>I haven't ever needed to implement copy(), I imagine that its purpose is for
>RTTI.


there is also clone.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      04-26-2004
VisionSet wrote:

> Is there any kind of convention that dictates where it is usual to perform
> shallow and deep copies?


I'd say you perform deep copies only where necessary, and shallow copies
everywhere else. The trick is to determine the meaning of "necessary"
for your application, and that may not be straightforward.

> I prefer to use copy constructors to implement any kind of copy, but now I
> find the need for a shallow and deep copy in the same class.


That may well be, but I am inclined to suspect that the need could be
removed by a round of refactoring. I would be nervous at the prospect
of putting both behaviors on the same class because there is then great
opportunity for users to accidentally confuse them.

> I haven't ever needed to implement copy(), I imagine that its purpose is for
> RTTI.


You mean "clone()", you wrote elsewhere. In that context then, do you
mean you think the purpose is to _avoid_ the need for RTTI? In that I
would partially agree with you -- only partially, because you still have
to worry about whether the objects in question are Cloneable. Also, if
you plan for such a feature in your own classes then you can usually
implement it in a polymorphic way without using clone.


John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-27-2004
On Mon, 26 Apr 2004 09:11:55 -0500, "John C. Bollinger"
<(E-Mail Removed)> wrote or quoted :

>I'd say you perform deep copies only where necessary, and shallow copies
>everywhere else. The trick is to determine the meaning of "necessary"
>for your application, and that may not be straightforward.


If your objects are immutable that you reference, then shallow copy
will suffice. You may find it easier to create new immutable objects
as needed than to deal with deep copies.

Deep copies are a bit like pulling on a thread on a sweater. It can
just keep going and going unexpectedly far.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      04-27-2004
Roedy Green wrote:

> On Mon, 26 Apr 2004 09:11:55 -0500, "John C. Bollinger"
> <(E-Mail Removed)> wrote or quoted :
>
>>I'd say you perform deep copies only where necessary, and shallow copies
>>everywhere else. The trick is to determine the meaning of "necessary"
>>for your application, and that may not be straightforward.

>
>
> If your objects are immutable that you reference, then shallow copy
> will suffice. You may find it easier to create new immutable objects
> as needed than to deal with deep copies.


Yes, that's a good rule where it's applicable. Unfortunately there are
far too many situations where it isn't. Good design may reduce the
number, but it may not be able to eliminate them all.

> Deep copies are a bit like pulling on a thread on a sweater. It can
> just keep going and going unexpectedly far.


Quite right.


John Bollinger
(E-Mail Removed)

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      04-27-2004
John C. Bollinger wrote:
> Roedy Green wrote:


> > Deep copies are a bit like pulling on a thread on a sweater. It can
> > just keep going and going unexpectedly far.

>
> Quite right.


And a lovely analogy too.

-- chris


 
Reply With Quote
 
Tris Orendorff
Guest
Posts: n/a
 
      04-29-2004
"VisionSet" <(E-Mail Removed)> wrote in
news:s3Uic.166$cy3.127@newsfe1-win:

> Is there any kind of convention that dictates where it is usual to
> perform shallow and deep copies?
> I prefer to use copy constructors to implement any kind of copy, but
> now I find the need for a shallow and deep copy in the same class.


Hi,

I only use deep copy when a method is recursive -- perhaps the board state of a game or some other
complex object where anything can be changed. I have never needed both shallow and deep cloning in
the same class before.

--
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS d++ s+:- a+ C+ UL++++ P+ L+ E- W+ N++ o- K++ w+ O+ M !V PS+ PE Y+ PGP t+ !5 X- R- tv--- b++
DI++ D+ G++ e++ h---- r+++ y+++
------END GEEK CODE BLOCK------

 
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
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
Copy constructor: why can't I copy objects as if they were structs? rdc02271 C++ 24 12-27-2005 12:38 PM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments