Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > deep and shallow copy

Reply
Thread Tools

deep and shallow copy

 
 
Tony Johansson
Guest
Posts: n/a
 
      05-19-2005
Hello!

I'm reading in a book about C++ and that is something that sound strange.

It says "Pointers have reference-assignment semantics similar to those in
Java. For example, after the assignment
Student* john = michael;
both john and michael share the same object. This type of an assignment is
different then value-assignmnet semantics used by class variables, as in

Student kasia(10);
Student barbara(11);
......
kasia = barbara;
The result of the above assinmnet is a memberwise copy of all class
attributes which for this example is a copy of the attribute number_.

Now to my question what will the book mean by this sentence marked* below?
Does they mean that this assignment Student* john = michael; is a shallow
copy and this assignment
kasia = barbara; is a deep copy or what do they mean???

*The two types of assignment, reference and value, are also known as a
shallow copy and a deep copy, respectively.

Many thanks!

//Tony


 
Reply With Quote
 
 
 
 
msalters
Guest
Posts: n/a
 
      05-19-2005
Tony Johansson wrote:
> Hello!
>
> I'm reading in a book about C++ and that is something that sound

strange.
>
> It says "Pointers have reference-assignment semantics ...
> This type of an assignment is different then value-
> assignmnet semantics used by class variables


> Now to my question what will the book mean by this sentence marked*

below?

> Does they mean that this assignment
> Student* john = michael;
> is a shallow copy and this assignment
> kasia = barbara;
> is a deep copy.
>
> *The two types of assignment, reference and value,
> are also known as a shallow copy and a deep copy,
> respectively.


Yes. You got it right. The first one is called shallow,
because it doesn't actually change a Student object.
Only the (small) Student* pointer called john is
initialized. (not changed, the pointer didn't have
a value, but still a shallow copy).
The second one is called deep, because it actually
changes the bits inside the Student object. The kasia
object held an old value, but this value is discarded
and replaced with a copy of the barbara object. This
is thus a true assignment.

Somewhat more advanced tests (shallow or deep?)

Student Alice,Bob,Charlie;

Student oldest = Alice; // shallow||deep?
Student* youngest = & Charlie; // shallow||deep?
Student& tallest = Bob; // shallow||deep?
Student notSoOld = *youngest; // shallow||deep?

HTH,
Michiel Salters

 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      05-19-2005

"msalters" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Tony Johansson wrote:
>> Hello!
>>
>> I'm reading in a book about C++ and that is something that sound

> strange.
>>
>> It says "Pointers have reference-assignment semantics ...
>> This type of an assignment is different then value-
>> assignmnet semantics used by class variables

>
>> Now to my question what will the book mean by this sentence marked*

> below?
>
>> Does they mean that this assignment
>> Student* john = michael;
>> is a shallow copy and this assignment


No, it's not a copy at all, it's a pointer assignment.

>> kasia = barbara;
>> is a deep copy.



That depends on the contents of the object type, and whether there is an
assignment operator defined.

>>
>> *The two types of assignment, reference and value,
>> are also known as a shallow copy and a deep copy,
>> respectively.

>
> Yes. You got it right. The first one is called shallow,
> because it doesn't actually change a Student object.
> Only the (small) Student* pointer called john is
> initialized. (not changed, the pointer didn't have
> a value, but still a shallow copy).


No, that's not correct. That's simply a pointer assignment. The pointer
john is changed to point to the same memory location as michael. That's not
a copy at all.

A "shallow" copy is when the member values are physically copied from one
object to another, *including* the values of any pointer or reference
members. If there are pointer or reference memebrs, then, those poointers
or references refer to the *same* objects as the original object, which is
usually a bad thing. That's why you want to define a copy constructor and
assignment operator for objects that contain pointers or references.

It's called a "shallow" copy because only the values of the
pointers/references are copied, instead of making copies of those
referred-to objects and setting pointers to them. *That* is what would be
called a "deep" copy, because it's going "deeper" into the structure,
copying everything, not just the first "layer".


> The second one is called deep, because it actually
> changes the bits inside the Student object. The kasia
> object held an old value, but this value is discarded
> and replaced with a copy of the barbara object. This
> is thus a true assignment.
>


It's an assignment, but whether it's a "deep" copy (i.e., a member-wise
copy), requires more information.

> Somewhat more advanced tests (shallow or deep?)
>
> Student Alice,Bob,Charlie;
>
> Student oldest = Alice; // shallow||deep?
> Student* youngest = & Charlie; // shallow||deep?
> Student& tallest = Bob; // shallow||deep?
> Student notSoOld = *youngest; // shallow||deep?


As stated above, the difference between a shallow copy and a deep copy is
going to depend on the type of members the object contains, and whether a
copy constructor and/or assignment operator is defined. The default
assignment operator does a "bit-wise" copy, which is a "shallow" copy when
there are pointer or reference members (or members of members, for that
matter).

-Howard




 
Reply With Quote
 
Rapscallion
Guest
Posts: n/a
 
      05-19-2005
Howard wrote:
> "msalters" <(E-Mail Removed)> wrote
> > Tony Johansson wrote:
> >> Does they mean that this assignment
> >> Student* john = michael;
> >> is a shallow copy and this assignment

>
> No, it's not a copy at all, it's a pointer assignment.


Right!

> >> kasia = barbara;
> >> is a deep copy.

>
> That depends on the contents of the object type, and whether there is

an
> assignment operator defined.


Right again! The copy of a Student hardly makes sense. Student is a
non-copyable entity object. The difference between entity- and
value-objects is hardly known among programmers.

R.C.

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      05-19-2005

"Rapscallion" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Howard wrote:
>> "msalters" <(E-Mail Removed)> wrote
>> > Tony Johansson wrote:
>> >> Does they mean that this assignment
>> >> Student* john = michael;
>> >> is a shallow copy and this assignment

>>
>> No, it's not a copy at all, it's a pointer assignment.

>
> Right!
>
>> >> kasia = barbara;
>> >> is a deep copy.

>>
>> That depends on the contents of the object type, and whether there is

> an
>> assignment operator defined.

>
> Right again! The copy of a Student hardly makes sense. Student is a
> non-copyable entity object. The difference between entity- and
> value-objects is hardly known among programmers.
>


? I hate to question someone who's agreeing with me, but you lost me there.
Is this all referring to some code that wasn't posted in the original
question? I see nothing that said that the Student object was non-copyable.
Are you just referring to the "concept" of a Student, and saying that "a
student is not copyable, because you can't copy humans"? Or am I missing
something?

-Howard





 
Reply With Quote
 
Rapscallion
Guest
Posts: n/a
 
      05-19-2005
Howard wrote:
> ? I hate to question someone who's agreeing with me, but you lost me

there.
> Is this all referring to some code that wasn't posted in the original


> question? I see nothing that said that the Student object was

non-copyable.

Ok

> Are you just referring to the "concept" of a Student, and saying that

"a
> student is not copyable, because you can't copy humans"? Or am I

missing
> something?


Not (only) because you can't copy humans but because it makes no sense
to copy 'Students', or more general, objects that have an identity.
What should e.g. 'kasia = barbara;' mean? It's semantically wrong (of
course, technically you can implement a copy constructor).

R.C.

 
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
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
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/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM



Advertisments