Shallow copy and Deep copy


A shallow copy of an object copies all of the member field values. This works well if the fields are values, but may not be what you want for fields that point to dynamically allocated memory. The pointer will be copied. but the memory it points to will not be copied -- the field in both the original object and the copy will then point to the same dynamically allocated memory, which is not usually what you want. The default copy constructor and assignment operator make shallow copies.

A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor and overload the assignment operator, otherwise the copy will point to the original, with disastrous consequences.

4 comments:

  1. Life is like a landscape. You live in the midst of it but can describe it only from the vantage point of distance. See the link below for more info.

    #distance
    www.inspgift.com

    ReplyDelete

  2. Time is gold. We should treasure every second of it so that in time we will not regret anything because we only live ones.I have read your article and i was glad and inspired. Thanks for it, Cheers!
    Visit my site if you have time, Enjoy.

    n8fan.net

    www.n8fan.net

    ReplyDelete
  3. I will keep your new article. I really enjoyed reading this post, thanks for sharing.

    Sol
    www.gofastek.com

    ReplyDelete
  4. I will keep your new article. I really enjoyed reading this post, thanks for sharing.

    Sol
    www.gofastek.com

    ReplyDelete