Boost logo

Boost :

Subject: Re: [boost] Interest in boost.deepcopy
From: Simonson, Lucanus J (lucanus.j.simonson_at_[hidden])
Date: 2011-10-25 17:33:49


>Consider an object hierarchy in which there may be multiple references to
>shared objects. A deep copy should give an identical, but entirely separate
>copy of the entire structure.

>Also consider eg std::map<std::string,T*>... the std::map copy constructor
>is not going to create new instances of T in the copy - this is a shallow
>copy.

>You could argue that I should write a custom smart pointer to perform a deep
>copy in its copy constructor, but for multiple reasons I'd quite like to not
>have to do this, and to use boost::shared_ptr as normal.

No smart pointer, a separate class for each instance of the need for deep copy. You have a missing object in your design that owns the "entire structure" and implements the deep copy as its assignment operator. That object is responsible for de-duplicating pointers to shared objects. Whenever this comes up in whatever guise, a C++ garbage collector or smarter smart pointer, the answer is always the same: get the ownership of memory clear in you design and use RAII to manage memory and assignment operator to make copies. You shouldn't be trying to make sense of cyclical dependencies of raw pointers in the first place. Fix the bad design, don't enable it with a clever library.

Regards,
Luke


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk