deep and shallow copy

T

Tony Johansson

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
 
M

msalters

Tony said:
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
 
H

Howard

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


That depends on the contents of the object type, and whether there is an
assignment operator defined.
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
 
R

Rapscallion

Howard said:
No, it's not a copy at all, it's a pointer assignment.
Right!


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

Howard

Rapscallion said:
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
 
R

Rapscallion

Howard said:
? 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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top