How to implement a deep copy or shallow copy?

S

shuisheng

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
 
O

Ondra Holub

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.
 
S

Salt_Peter

shuisheng said:
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?=

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.
 
U

Uenal S. Mutlu

shuisheng said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top