smart_ptr: Helper to ease handling objects from a class heirarchy

Hi, I was thinking whether it would be meaningful to have a template<typename T, typename Clone_policy> class Owned_ptr { ... }; where: 1) The Owned_ptr takes care of object ownership like other smart pointers; deleting the object when the Owned_ptr is destructed. 2) The assignment operator doesn't transfer the ownership rights, but clones the object using the Clone_polcy. This allows using the copy constructor or a virtual clone function in the class heirarchy. 3) The copy constructor works just like assignment. The recent projects that I've worked own have benefitted from such an helper class. It eases creating deep copies of complex objects; avoids having to implement assignment operators/copy constructors for every class. Best regards, Anand.

I've found a use for (2) but a better name for that use case would be cloning_ptr or something. Essentially the point was to avoid manually writing copy constructors/assignment/destructors when I needed to hold POD types by pointer. so something like: struct c_lib_struct; c_lib_struct * new_c_lib_struct(..); c_lib_struct * copy_c_lib_struct(c_lib_struct *); cloning_ptr<c_lib_struct>(new_c_lib_struct(..),copy_c_lib_struct,free_c_lib_struct); Though not quite - took a couple of shortcuts ;-) -----Original Message----- From: boost-bounces@lists.boost.org on behalf of Anand Shankar Sent: Sat 3/24/2007 2:03 PM To: boost@lists.boost.org Subject: [boost] smart_ptr: Helper to ease handling objects from a classheirarchy 2) The assignment operator doesn't transfer the ownership rights, but clones the object using the Clone_polcy. This allows using the copy constructor or a virtual clone function in the class heirarchy.

Hi Sohail, cloning_ptr<c_lib_struct>(new_c_lib_struct(..),copy_c_lib_struct,free_c_lib_struct); This leads me to belive that in the case you mention, the cloning_ptr stores function pointers for cloning and object deletion. Your example is interesting because it requires that the `delete mechanism' be supplied as a template policy parameter as well. template<typename T, typename Clone_policy=Use_new, typename Delete_policy=Use_delete> class Cloned_ptr { ... }; Here Use_new, Use_delete help default to using new and copy constructor for cloning and using delete for object deletion. I've had to write such a class and also something very similar to Boost::ptr_vector in course of my work. Best regards, Anand.
participants (2)
-
Anand Shankar
-
Sohail Somani