What is difference between "owned pointer" and the "stored pointer" for std::shared_ptr?
What is the difference between "owned pointer" and the "stored pointer" for
std::shared_ptr
?
Anytime you use the constructor template< class Y > shared_ptr::shared_ptr( const shared_ptr<Y>& r, element_type* ptr ) noexcept;
, you have something that shares ownership with r
, but dereferences to *ptr
.
E.g. after
std::shared_ptr<std::pair<int, double>> pair = std::make_shared<std::pair<int,double>>(1, 2.);
std::shared_ptr<int> i(pair, &pair->first);
pair.reset();
the std::pair<int, double>
is kept alive by i