By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,110 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,110 IT Pros & Developers. It's quick & easy.

How to implement a deep copy or shallow copy?

P: n/a
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

Dec 16 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

Dec 16 '06 #2

P: n/a

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.

Dec 16 '06 #3

P: n/a
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
Dec 16 '06 #4

P: n/a
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.
Dec 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.