Boost logo

Boost Users :

From: bill_kempf (williamkempf_at_[hidden])
Date: 2002-07-11 16:38:14


--- In Boost-Users_at_y..., "Peter Dimov" <pdimov_at_m...> wrote:
> From: "Mark Storer" <mstorer_at_c...>
> > So the only purpose of a weak_ptr is to be able to grab a
shared_ptr from
> it
> > (and to avoid circular references, of course)?
>
> Yes. The "idiomatic" use is:
>
> weak_ptr<X> wp;
>
> // ...
>
> if(shared_ptr<X> p = make_shared(wp))
> {
> // use p
> }
> else
> {
> // target lost
> }

Well, you could write the weak_ptr<>::operator-> to "do the right
thing":

shared_ptr<T> weak_ptr<T>::operator->()
{
   shared_ptr<T> p = make_shared(wp);
   if (!p)
     throw "something";
   return p;
}

I can see several reasons to not want to do this (mostly to do with
performance), though.

Bill Kempf


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