Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2005-08-16 08:27:23


Martin Bonner <martin.bonner_at_[hidden]> writes:

> 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.

Right, I went through the same thing. The documentation claims a bit
too much for this library. I expected to find some magic in the
library but instead I found what I already knew to be the practical
limitation of C++: the copying logic is captured based on the static
type of the pointer with which the original smart pointer was
initialized, not on the dynamic type of the object referred to.

> 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's basically the same rules as shared_ptr, so I don't think it's too
bad. However, we're reviewing Dave and Andrei's policy_ptr soon so it
might be better to use that framework for something like this.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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