Hmm -- so I guess, in the cases I&#39;m talking about, I should store a shared_ptr, and just return a const refererence.<div>Then, if I need to have a copy of the member elsewhere to manipulate, I could return a weak_ptr.</div>

<div>Thanks for that!<br><br><div class="gmail_quote">On 2 July 2011 18:06, Richard Damon <span dir="ltr">&lt;<a href="mailto:Richard@damon-family.org">Richard@damon-family.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">On 7/2/11 12:28 PM, doug livesey wrote:<br>
&gt; Hi -- I&#39;m loving how easy shared_ptr makes object handling, but am not<br>
&gt; sure about how to use weak_ptr.<br>
&gt; Should I use it in places where previously I would have, say, returned<br>
&gt; a const reference?<br>
&gt;<br>
</div>A weak pointer should be used where you need to keep a pointer around<br>
for an object, but you do not insist that the object stays around, but<br>
you do want to know if the object has gone away.<br>
<br>
The main use of this is circular references, where for example, object A<br>
has a pointer to B, and object B has a pointer to A. If both of these<br>
were regular shared_ptrs than once this combination was created, then it<br>
would never go away as both A and B have a strong reference to them.<br>
Making one of the links a weak pointer, says that when all other<br>
shared_ptr references to the pair go away, then both A and B go away.<br>
<br>
You do have to be a bit careful and think about the consequences, and if<br>
you change the pointer from B to A to be a weak-ptr, then if all<br>
shared_ptr references to A go away, but you still have some to B, then A<br>
will go away but not B, so B needs to be able to handle that case.<br>
<font color="#888888"><br>
--<br>
Richard Damon<br>
<br>
_______________________________________________<br>
Boost-users mailing list<br>
<a href="mailto:Boost-users@lists.boost.org">Boost-users@lists.boost.org</a><br>
<a href="http://listarchives.boost.org/mailman/listinfo.cgi/boost-users" target="_blank">http://listarchives.boost.org/mailman/listinfo.cgi/boost-users</a><br>
</font></blockquote></div><br></div>

