|
Boost : |
From: Axter (boost_at_[hidden])
Date: 2005-08-16 19:54:00
----- Original Message -----
From: "Martin Bonner" <martin.bonner_at_[hidden]>
Newsgroups: gmane.comp.lib.boost.devel
Sent: Tuesday, August 16, 2005 6:17 AM
Subject: Re: Propose adding Clone Smart Pointer (clone_ptr) to the boost
library
> ----Original Message----
> From: Axter [mailto:boost_at_[hidden]]
> Sent: 16 August 2005 03:39
> To: boost_at_[hidden]
> Subject: [boost] Propose adding Clone Smart Pointer (clone_ptr) to the
> boost library
>
>> I'm proposing to add the clone_ptr class to the boost library.
>>
>> I've developed a clone_ptr class that performs a clone of an abstract
>> pointer without the need for a clone method.
>>
>> The clone_ptr is well suited for automatic deep copy of abstract
>> pointers,
>> and for use with STL containers.
>>
>> In addition to being able to perform clone operations, it also have
>> operators that performs comparison on the object being pointed to,
>> instead
>> of the pointer address.
>> This allows for correct sorting and comparison of clone_ptr's in an
>> STL
>> container.
>> See following link for more info:
>> http://code.axter.com/clone_ptr_introduction.htm
>
> I was interested in the example you give in the web page:
>
> class foo
> {
> clone_ptr < AbstractClass > m_MyAbstractClassPtr;
> public:
> foo(AbstractClass *
> abstractclassptr_):m_MyAbstractClassPtr(abstractclassptr_){}
>
> foo(const foo& Src)
> {
> //In the following line of code, the clone pointer will copy
> the
> // correct derived class without the need of a clone method
> m_MyAbstractClassPtr = Src.m_MyAbstractClassPtr;
> }
> };
>
> That looked a very clever trick. I couldn't see how clone_ptr was going
> to
> copy the dynamic type of the argument to the constructor without any
> information about that dynamic type.
>
> Having looked at the code, I don't think it does. It would work if the
> constructor was declared:
> foo(DerivedClass*p): m_MyAbstractClassPtr(p) {}
> or even
> template <class Derived>
> foo(Derived *p):m_MyAbstractClassPtr(p) {}
>
> but even that will fail if the constructor is called via:
> Derived *p = new DerivedDerived;
> foo foo_obj(p);
>
> My basic concern is that while clone_ptr could be useful, it only works if
> people stick rigidly to certain rules. Those rules need to be made VERY
> clear, otherwise it provides a plentiful supply of rope.
>
> It would also be useful to have some documentation of what methods
> clone_ptr
> provides, and what effects they have.
> --
> Martin Bonner
> Martin.Bonner_at_[hidden]
> Pi Technology, Milton Hall, Ely Road, Milton, Cambridge, CB4 6WZ,
> ENGLAND Tel: +44 (0)1223 441434
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
The documents state that the clone_ptr class is based on strict pointer
ownership logic.
The above code is not using strict ownership logic, and instead the object
is created using a dumb pointer.
If the clone_ptr is used with strict ownership logic, and object is created
via new directly to the constructor it works with no problem.
But you're absolutely correct, in that there's nothing to stop some one from
doing it wrong.
If you read common documents on the web about clone pointers, they usually
describe a clone pointer with strict ownership logic.
I also think you're right in that I should add more to the documentation
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk