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

deep and shallow copy

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


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

Jul 23 '05 #2

P: n/a

"msalters" <Mi*************@logicacmg.com> wrote in message
news:11*********************@z14g2000cwz.googlegro 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


Jul 23 '05 #3

P: n/a
Howard wrote:
"msalters" <Mi*************@logicacmg.com> 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.

Jul 23 '05 #4

P: n/a

"Rapscallion" <ra********@spambob.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
Howard wrote:
"msalters" <Mi*************@logicacmg.com> 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

Jul 23 '05 #5

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

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.