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 5 3286
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,Charl ie;
Student oldest = Alice; // shallow||deep?
Student* youngest = & Charlie; // shallow||deep?
Student& tallest = Bob; // shallow||deep?
Student notSoOld = *youngest; // shallow||deep?
HTH,
Michiel Salters
"msalters" <Mi************ *@logicacmg.com > wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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,Charl ie;
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
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.
"Rapscallio n" <ra********@spa mbob.com> wrote in message
news:11******** *************@g 43g2000cwa.goog legroups.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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 assignment operator? So far I can't find much useful
in the regular documentation. I understand the basic concept that
names are bound to objects which exist on the heap, but that still
doesn't explain why a = b doesn't _always_ cause a to point to the...
|
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
>>>
|
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)
10.What's the difference between the System.Array.CopyTo() and
System.Array.Clone()?
|
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:
http://blogs.msdn.com/brada/archive/2004/05/03/125427.aspx
This artivle seems to hint that I should not use System.IClonable but
instead define my own interface(s) for cloning. Now since this article
is rather old and since they did not obsolete IClonable there might be
a new "best practise".
|
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
| |
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 different.)
It's hard for me to recall the question exactly. But basically, it
involved a class construction with a shallow copy constructor.
The question was basically "What is the bug in this code?" The
intended answer was that shallow...
|
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 to make it more
manageable.
Below is a draft of a document that I plan to give to my introductory
C++ class.
Please note that I have purposely left out any mention of safety issues
in the ctors which could be resolved thru some combination...
|
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
|
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 format, 3 pp)
My question, coming from a C++ background where deep copying is done,
is why in C# you would do either deep or shallow copying as suggested
by O'Reilly (using the "ICloneable" inhereited interface), at least
for the .NET framework.
...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |