Boost logo

Boost Users :

From: lists.boost.org_at_[hidden]
Date: 2007-11-09 04:13:56


Hi Ovanes

Thanks for your reply.
Yes, I understand that do_clone is private.

I wonder what Peter Dimov's idea is.
I believe the point of this free function would be that you would only need to write it once and use it for any class that provides the do_clone() method.
Which I'd love to see in action. Or be proven wrong.

Thanks
Bernhard

On Thu, 8 Nov 2007, Ovanes Markarian wrote:

> Hi!
>
> do_clone is really private ;) Your derived class has public clone
> member and you should use it from the free function, but no the
> do_clone.
>
>
> Regards,
> Ovanes
>
>
>
> On 8 Nov 2007 14:53:49 +0100, Bernhard Wagner boost
> <lists.boost.org_at_[hidden]> wrote:
> In this post Peter Dimov shows a solution for the covariant return type
> problem when using shared_ptr:
> http://lists.boost.org/boost-users/2003/02/2996.php
>
> I'm interested in the free function solution he provides:
>
> > [snip] you can use the usual pattern from the no-covariant-return
> days:
> >
> > class B
> > {
> > virtual shared_ptr<B> do_clone() const = 0;
> > };
> >
> > class X: public B
> > {
> > virtual shared_ptr<B> do_clone() const { return shared_ptr<X>(new
> > X(*this)); }
> >
> > public:
> >
> > shared_ptr<X> clone() const
> > {
> > shared_ptr<X> px = dynamic_pointer_cast<X>(this->do_clone());
> > assert(px);
> > return px;
> > }
> > };
> >
> > It buys you some extra safety, too.
> >
> > You could also use a free function:
> >
> > template<class X> shared_ptr<X> clone(X const & x)
> > {
> >
> > shared_ptr<X> px = dynamic_pointer_cast<X>( x.do_clone());
> > assert(px);
> > return px;
> > }
> >
>
> So, to clone an object using the clone method, I could use:
>
> X* x;
> shared_ptr<X> spX(x = new X());
> shared_ptr<X> spX2 = spX->clone();
> // or:
> shared_ptr<X> spX3 = x->clone();
>
> But how is the free function meant to be used?
>
> This provokes the compiler error: '... do_clone() const is private ...':
> shared_ptr<X> spX4 = clone(*spX);
>
> Thank you
> Bernhard
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

-- 
  Bernhard Wagner                                 bernhard.wagner_at_[hidden]
  Milchbuckstr. 14                                   VOICE +41-1-364 50 03
  CH-8057 Zurich, Switzerland                     http://listen.to/bernard

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net