473,216 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 software developers and data experts.

deep and shallow copy

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
5 3237
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

"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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: dan | last post by:
without stirring the pot too much -- could someone please point me to whatever documentation exists on the philosophy, semantics, and practical implications of how Python implements the...
2
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
4
by: fperfect13 | last post by:
Hi, I wanted to perform a deep copy of an array. Searching on google I ran into different opinions : C# Interview Questions (http://blogs.wwwcoder.com/tsvmadhav/archive/2005/04/08/2882.aspx)...
2
by: bonk | last post by:
I have come across the need to distinguish between the creation of a deep and a shallow copy and with great interest I have read this article: ...
26
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
5
by: pauldepstein | last post by:
I recently had a job interview question which I totally failed. (The question seemed excellent from an objective point of view, but having completely failed to do it, my subjective feelings are...
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
4
by: shuisheng | last post by:
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
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.