Connecting Tech Pros Worldwide Forums | Help | Site Map

How to implement a deep copy or shallow copy?

shuisheng
Guest
 
Posts: n/a
#1: Dec 16 '06
Dear All,

Is there any easy way to make sure all my object copies are deep copy
or shallow copy? I do not like to implement it in each class one by
one.

Thanks,

Shuisheng


Ondra Holub
Guest
 
Posts: n/a
#2: Dec 16 '06

re: How to implement a deep copy or shallow copy?


shuisheng napsal:
Quote:
Dear All,
>
Is there any easy way to make sure all my object copies are deep copy
or shallow copy? I do not like to implement it in each class one by
one.
>
Thanks,
>
Shuisheng
If you want to ensure, that all attributes of class are deep copied,
you should use only instances of object with correct assignment
operator. If you want to use pointers, you can use some kind of smart
pointer which implements deep copy.

But I do not think there is anything, what gives to you such
functionality without (almost) any work.

Salt_Peter
Guest
 
Posts: n/a
#3: Dec 16 '06

re: How to implement a deep copy or shallow copy?



shuisheng wrote:
Quote:
Dear All,
>
Is there any easy way to make sure all my object copies are deep copy
or shallow copy? I do not like to implement it in each class one by
one.
>
Then consider a redesign of your class(es).
In the case your problem involves pointers, for example, then stop
using pointers. If you are allocating members, then stop allocating
members. The alternatives makes code simpler, safer and more
maintainable. Unfortunately, you've not explained your delimna with
enough details to offer a resolution to your issue.

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
 
Posts: n/a
#4: Dec 16 '06

re: How to implement a deep copy or shallow copy?


On 2006-12-16 19:53, shuisheng wrote:
Quote:
Dear All,
>
Is there any easy way to make sure all my object copies are deep copy
or shallow copy? I do not like to implement it in each class one by
one.
To make sure that all copies made are deep copies don't use dynamic
memory for the members of the classes. For shallow copy make sure that
you use dynamic memory for the members and overload the copy-constructor
and assignment-operator (don't forget the destructor).

If you want to allow a class to be either deep or shallow you'll have to
do a bit more work, perhaps use templates to parameterize the class and
provide two copy-constructors and assignment-operators, one for deep and
one for shallow.


I don't think there's any generic way that will apply to all classes
since even in a shallow copy there are usually some things that should
be copied deep.

--
Erik Wikström
Uenal S. Mutlu
Guest
 
Posts: n/a
#5: Dec 17 '06

re: How to implement a deep copy or shallow copy?


shuisheng wrote:
Quote:
Dear All,
>
Is there any easy way to make sure all my object copies are deep copy
or shallow copy? I do not like to implement it in each class one by
one.
>
Thanks,
>
Shuisheng
>
Thought you were taking porn there for a minute - deep throat and swallow.
Closed Thread