Quick A: No. Remember that weak_ptr
and *
are both valid non-owning pointers. Use * when you know the pointed-at object will outlive this pointer. Use weak_ptr
when you don't know whether the pointed-at object will outlive this pointer, and the pointed-at object is owned by shared_ptr
s.
For more good information, see the top answer to this question yesterday on SO:
Smart pointers + cycles + “->”
Sometimes I'm really sure that I want to have circular dependence of pointers, and every object on cycle should be able to use his pointer (so it can't be
weak_ptr
).My question is: Does this mean that I have bad design?
What if I want to implement graph? Can I use smart pointers? In graphs there are cycles, but with
weak_ptr
I can't use "->
". What can I do?I read some articles, reference and topics on StackOverflow, but it looks like I still don't get smart pointers. Really, why doesn't exists some variant of
weak_ptr
with "->
"?
Add a Comment
Comments are closed.