Boost logo

Boost Users :

Subject: Re: [Boost-users] How & when to use weak_ptr
From: Igor R (boost.lists_at_[hidden])
Date: 2011-07-02 15:29:28

> Hmm -- so I guess, in the cases I'm talking about, I should store a
> shared_ptr, and just return a const refererence.

Note that it might be rather dangerous, if your caller would store
such a const reference.

> Then, if I need to have a copy of the member elsewhere to manipulate, I
> could return a weak_ptr.

Returning weak_ptr usually doesn't make sense (except for the case
where you return it for tracking only). Note that shared_ptr is
implicitly convertible to weak_ptr, so typically you return
shared_ptr, and let the caller to decide what to do with it: if the
caller indends to store it, but doesn't want strong referencing, it
stores it as weak_ptr.

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at