Re: [boost] Propose adding Clone Smart Pointer (clone_ptr) to the boost library

----Original Message---- From: Jonathan Wakely [mailto:cow@compsoc.man.ac.uk] Sent: 17 August 2005 14:17 To: boost@lists.boost.org Subject: Re: [boost] Propose adding Clone Smart Pointer (clone_ptr) to theboost library
Axter wrote:
It does not need a clone member if you apply strict pointer ownership logic, which is what a clone pointer normally does.
clone_ptr also needs the definitions of all derived classes to be visible, whereas something like ptr_container that uses a clone() member function only needs to see the definition of the base class and can do everything through the base class' interface.
I think it only needs the definition available for the constructor (which is where a class derived from his "copier" class is instantiated). [snip]
This would not be possible with std::vector<clone_ptr<Base> >:
// pc.h #include <boost/ptr_container/ptr_vector.hpp> struct Base { ~Base() {} virtual Base* clone() = 0; virtual void f() = 0; }; void fill(boost::ptr_vector<Base>&);
// pc.cc #include "pc.h" #include <boost/bind.hpp> #include <algorithm>
int main() { boost::ptr_vector<Base> v; fill(v); std::for_each(v.begin(), v.end(), boost::bind(&Base::f, _1)); return 0; }
The fill() function can now be implemented in another file and populate the vector with any type derived from Base, without recompiling pc.cc
I think that is supported with clone_ptr. The file implementing fill would need the full definition of the derived type(s) of course, but that is less restrictive. -- Martin Bonner Martin.Bonner@Pitechnology.com Pi Technology, Milton Hall, Ely Road, Milton, Cambridge, CB4 6WZ, ENGLAND Tel: +44 (0)1223 441434

Martin Bonner wrote:
From: Jonathan Wakely
clone_ptr also needs the definitions of all derived classes to be visible, whereas something like ptr_container that uses a clone() member function only needs to see the definition of the base class and can do everything through the base class' interface.
I think it only needs the definition available for the constructor (which is where a class derived from his "copier" class is instantiated). [snip]
This would not be possible with std::vector<clone_ptr<Base> >: [snip bad example] The fill() function can now be implemented in another file and populate the vector with any type derived from Base, without recompiling pc.cc
I think that is supported with clone_ptr. The file implementing fill would need the full definition of the derived type(s) of course, but that is less restrictive.
And to be expected ... yes, you're right. I might have had another example in mind originally but can't think of it now. I remove my objection! jon
participants (2)
-
Jonathan Wakely
-
Martin Bonner