Quite often I work with an old code where raw pointers are mixed with smart ones and I don't have the time to change all raw ones to smart.
And there can be some situations like static raw pointer pointing to an object, which can be already destructed and at first it seems like a situation to use weak_ptr to hold the reference, but there the problem arises, because the place with raw pointer does not have any information about shared_ptrs already pointing to the same object.
So:
1) Is there any smarter smart pointer that tracks all pointers (both raw and smart) to an object?
2) Is there any smarter smart pointer that at least tracks all shared_ptrs to an object?
I don't want a discussion about an implementation, if possible I want to use it as a black-box.
EDIT: I asked 2), because for example calling make_shared on an object twice, makes 2 separate shared_ptr reference counters.
this
? If you answer like me "that's impossible to do", well, you got the reason why no such things exists... – cmaster - reinstate monicashared_ptr
s to an object given... a raw pointer to it? Or a shared one? – HolyBlackCatstd::shared_ptr
). That's whatstd::enable_shared_from_this
does. Note that some people (myself included) consider this an anti-pattern, but it is part-and-parcel of the C++ language by the standard. – EljayFoo foo;
and then doauto ptr = std::make_shared<Foo>(foo);
you have a pointer to a copy offoo
, i.e. a separate object – Caleth