|
Boost : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-06-24 05:21:47
From: "Markus Schöpflin" <markus.schoepflin_at_[hidden]>
> > So if you say
> >
> > shared_ptr<void> px(static_cast<X*>(0));
> >
> > when 'px' is destructed, it will invoke
> >
> > delete static_cast<X*>(0);
> >
> > I could have made the simple case
> >
> > shared_ptr<int> px(0);
> >
> > work, but it's better not to; it is important to understand the
difference.
> > And of course,
> >
> > shared_ptr<void> px(0);
> >
> > won't work anyway, since void is incomplete.
> >
>
> Do I understand you correctly? I can use a shared_ptr<void> as some kind
> of really smart pointer which will handle any type I happen to stuff
> into it?
In short, yes. A shared_ptr will delete the pointer that was passed to the
constructor, using its original type. So a shared_ptr<void> can keep, and
properly delete, a pointer to any object. A shared_ptr<X> can keep a pointer
to a class derived from X, and will properly delete it even if X's
destructor is not virtual. It's all part of the "just do the right thing and
don't bother me with details" philosophy. :-)
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk