I'm not sure that I suffer more from documentation errors or a headache, so ...
What I want to do is create a shared_ptr that shares ownership with another, but which refers to an element of the object, not the entire object. Simple example, starting point ...
struct s { int a, b; }; shared_ptr<s> s1 (new s);
From en.cppreference.com constructor (8) shared_ptr ...
template< class Y > shared_ptr( const shared_ptr<Y>& r, T *ptr );
The description mentions "Creates shared_ptr, which shares owner information with r, but contains an unrelated and unmanaged pointer ptr ... for example, in typical use cases, where ptr is a member of an object controlled by r".
So ... Was T just accidentally skipped from the template in this constructor, or am I missing something? In fact, Y looks just as wrong for me, since usually this constructor is correctly described?
What I hope I can do is something like this ...
shared_ptr<int> s2 (s1, &(s1.get ()->a));
s2 points to member a (an int ), but transfers ownership of the entire object using s1 .
Is this reasonable?
source share