deep and shallow copy

Discussion in 'C++' started by Tony Johansson, May 19, 2005.

  1. 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
     
    Tony Johansson, May 19, 2005
    #1
    1. Advertising

  2. Tony Johansson

    msalters Guest

    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
     
    msalters, May 19, 2005
    #2
    1. Advertising

  3. Tony Johansson

    Howard Guest

    "msalters" <> wrote in message
    news:...
    > 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
     
    Howard, May 19, 2005
    #3
  4. Tony Johansson

    Rapscallion Guest

    Howard wrote:
    > "msalters" <> 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.
     
    Rapscallion, May 19, 2005
    #4
  5. Tony Johansson

    Howard Guest

    "Rapscallion" <> wrote in message
    news:...
    > Howard wrote:
    >> "msalters" <> 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, May 19, 2005
    #5
  6. Tony Johansson

    Rapscallion Guest

    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.
     
    Rapscallion, May 19, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    8
    Views:
    4,947
    Tris Orendorff
    Apr 29, 2004
  2. Alex
    Replies:
    2
    Views:
    1,269
  3. Replies:
    1
    Views:
    558
    Victor Bazarov
    Jun 24, 2005
  4. Replies:
    26
    Views:
    2,157
    Roland Pibinger
    Sep 1, 2006
  5. shuisheng
    Replies:
    4
    Views:
    845
    Uenal S. Mutlu
    Dec 17, 2006
Loading...

Share This Page