Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   How to implement a deep copy or shallow copy? (http://www.velocityreviews.com/forums/t459271-how-to-implement-a-deep-copy-or-shallow-copy.html)

shuisheng 12-16-2006 06:53 PM

How to implement a deep copy or shallow copy?
 
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 12-16-2006 08:06 PM

Re: How to implement a deep copy or shallow copy?
 
shuisheng napsal:
> 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 12-16-2006 09:18 PM

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

shuisheng wrote:
> 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?= 12-16-2006 09:39 PM

Re: How to implement a deep copy or shallow copy?
 
On 2006-12-16 19:53, shuisheng wrote:
> 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 12-17-2006 08:40 AM

Re: How to implement a deep copy or shallow copy?
 
shuisheng wrote:
> 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.


All times are GMT. The time now is 01:24 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.