Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > what is Deep Copy, shallow copy and bitwises copy.?

Reply
Thread Tools

what is Deep Copy, shallow copy and bitwises copy.?

 
 
saxenavaibhav17@gmail.com
Guest
Posts: n/a
 
      08-30-2006
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy?
and what is the difference between them?


pls help
vaibhav

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      08-30-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy?
> and what is the difference between them?


What is a homework question? See this FAQ:

http://parashift.com/c++-faq-lite/ho...t.html#faq-5.2

Surely google can help you.

Cheers! --M

 
Reply With Quote
 
 
 
 
Roland Pibinger
Guest
Posts: n/a
 
      08-30-2006
On 30 Aug 2006 06:06:40 -0700, "mlimber" <(E-Mail Removed)> wrote:
>(E-Mail Removed) wrote:
>> what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy?
>> and what is the difference between them?

>
>What is a homework question? See this FAQ:


It doesn't sound like homework. The question is not covered in the C++
FAQ. I cannot quickly find a link with decent explanations. The
answers are not entirely trivial (e.g.: For which objects is a bitwise
copy appropriate? When is a memberwise copy shallow, when deep? ...).
Nobody should hold back answers.
BTW, copy constructors almost never need to be implemented but should
be made private (without implementation).

Best regards,
Roland Pibinger
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-30-2006
Roland Pibinger wrote:

<snip>

> BTW, copy constructors almost never need to be implemented but should
> be made private (without implementation).


why? Doesn't copy construction very often make semantic sense?


--
Nick Keighley

 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      08-30-2006
> what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy?

(1) Shallow Copy and Memberwise copy (They're equivalent)

struct MyStruct {
int *p1;
double *p2;
};

int main()
{
MyStruct obj1 = {new int,new double};

MyStruct obj2;

obj2 = obj1; /* Shallow or Memberwise */
}

(2) Bitwise Copy

#include <cstdlib>
using std::memcpy;

struct MyStruct {
int *p1;
double *p2;
};

int main()
{
MyStruct obj1 = {new int,new double};

MyStruct obj2;

memcpy(&obj2,&obj1,sizeof obj2); /* Shallow / Memberwise */
}

Note that a Bitwise Copy will also copy any padding between and after
members.

(3) Deep Copy

class MyClass {
int *p;

public:

MyClass() : p(new int) {}

MyClass(MyClass const &) : p(new int) {}
};

int main()
{
MyClass obj1,obj2;

obj2 = obj1; /* Deep Copy */
}

--

Frederick Gotham
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      08-30-2006
Frederick Gotham posted:


> memcpy(&obj2,&obj1,sizeof obj2); /* Shallow / Memberwise */



That comment should read /* Bitwise Copy */

--

Frederick Gotham
 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      08-30-2006
Frederick Gotham schrieb:
> (3) Deep Copy
>
> class MyClass {
> int *p;
>
> public:
>
> MyClass() : p(new int) {}


MyClass() : p(new int()) {}

>
> MyClass(MyClass const &) : p(new int) {}


MyClass(MyClass const &rhs) : p(new int(*rhs.p)) {}

Should copy something.

> };
>
> int main()
> {
> MyClass obj1,obj2;
>
> obj2 = obj1; /* Deep Copy */
> }
>


--
Thomas
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      08-30-2006
On Wed, 30 Aug 2006 17:33:50 GMT, Frederick Gotham
<(E-Mail Removed)> wrote:
>> what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy?

>
>(1) Shallow Copy and Memberwise copy (They're equivalent)


Are they equivalent? Doesn't 'memberwise' just mean 'member by member'
which may result in a deep or a shallow copy depending on the members.

Best wishes,
Roland Pibinger
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      08-30-2006
On 30 Aug 2006 08:50:43 -0700, "Nick Keighley"
<(E-Mail Removed)> wrote:
>Roland Pibinger wrote:
>> BTW, copy constructors almost never need to be implemented but should
>> be made private (without implementation).

>
>why? Doesn't copy construction very often make semantic sense?


For 'value types' the compiler-generated copy constructor is usually
the best. OTOH, objects (in the OO-sense) typically are non-copyable
and therfore need no copy constructor implementation, just a
private(ly) declared copy constructor (to make them non-copyable).

Best wishes,
Roland Pibinger
 
Reply With Quote
 
loufoque
Guest
Posts: n/a
 
      08-30-2006
Roland Pibinger wrote :

> OTOH, objects (in the OO-sense) typically are non-copyable


Please don't confuse your personal beliefs with programming theory.
I see no reason why objects would typically be non-copyable. Especially
in C++ which is based on copy semantics.
On the contrary, most objects should be copyable, unless they manage
some kind of unique resource (sockets, files, ...).

Moreover, if you want to make your object non-copyable clearly says so
instead of saying "write a private constructor". Actually, inheriting
from boost::noncopyable is probably clearer.
 
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
How to implement a deep copy or shallow copy? shuisheng C++ 4 12-17-2006 08:40 AM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
How to overload operator= to have both deep copy and shallow copy semantics bluekite2000@gmail.com C++ 1 06-24-2005 05:28 PM
deep and shallow copy Tony Johansson C++ 5 05-19-2005 04:13 PM
deep/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM



Advertisments