How to implement a deep copy or shallow copy?

Discussion in 'C++' started by shuisheng, Dec 16, 2006.

  1. shuisheng

    shuisheng Guest

    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
     
    shuisheng, Dec 16, 2006
    #1
    1. Advertising

  2. shuisheng

    Ondra Holub Guest

    shuisheng napsal:
    > 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


    If you want to ensure, that all attributes of class are deep copied,
    you should use only instances of object with correct assignment
    operator. If you want to use pointers, you can use some kind of smart
    pointer which implements deep copy.

    But I do not think there is anything, what gives to you such
    functionality without (almost) any work.
     
    Ondra Holub, Dec 16, 2006
    #2
    1. Advertising

  3. shuisheng

    Salt_Peter Guest

    shuisheng wrote:
    > 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.
    >


    Then consider a redesign of your class(es).
    In the case your problem involves pointers, for example, then stop
    using pointers. If you are allocating members, then stop allocating
    members. The alternatives makes code simpler, safer and more
    maintainable. Unfortunately, you've not explained your delimna with
    enough details to offer a resolution to your issue.
     
    Salt_Peter, Dec 16, 2006
    #3
  4. On 2006-12-16 19:53, shuisheng wrote:
    > 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.


    To make sure that all copies made are deep copies don't use dynamic
    memory for the members of the classes. For shallow copy make sure that
    you use dynamic memory for the members and overload the copy-constructor
    and assignment-operator (don't forget the destructor).

    If you want to allow a class to be either deep or shallow you'll have to
    do a bit more work, perhaps use templates to parameterize the class and
    provide two copy-constructors and assignment-operators, one for deep and
    one for shallow.


    I don't think there's any generic way that will apply to all classes
    since even in a shallow copy there are usually some things that should
    be copied deep.

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Dec 16, 2006
    #4
  5. shuisheng wrote:
    > 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
    >


    Thought you were taking porn there for a minute - deep throat and swallow.
     
    Uenal S. Mutlu, Dec 17, 2006
    #5
    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,949
    Tris Orendorff
    Apr 29, 2004
  2. Tony Johansson

    deep and shallow copy

    Tony Johansson, May 19, 2005, in forum: C++
    Replies:
    5
    Views:
    9,862
    Rapscallion
    May 19, 2005
  3. Alex
    Replies:
    2
    Views:
    1,273
  4. Replies:
    1
    Views:
    558
    Victor Bazarov
    Jun 24, 2005
  5. Replies:
    26
    Views:
    2,160
    Roland Pibinger
    Sep 1, 2006
Loading...

Share This Page