I’m not sure if I’m suffering more from a documentation error or a headache, so…
What I want to do is create a shared_ptr that shares ownership with another, but which references a member of the object instead of the whole object. Simple example, starting point…
struct s
{
int a, b;
};
shared_ptr<s> s1 (new s); // pointing to whole object
From en.cppreference.com, constructor (8) of shared_ptr is…
template< class Y >
shared_ptr( const shared_ptr<Y>& r, T *ptr );
The description mentions “Constructs a shared_ptr which shares ownership information with r, but holds an unrelated and unmanaged pointer ptr … such as in the typical use cases where ptr is a member of the object managed by r”.
So… Was T just accidentally missed from the template in that constructor, or am I missing something? In fact, Y looks like it’s wrong to me too, so just generally is that constructor described correctly?
What I’m hoping I can do is something like this…
shared_ptr<int> s2 (s1, &(s1.get ()->a));
s2 points to member a (an int), but shares ownership of the whole object with s1.
Is that sane?
The
Tparameter is a template parameter on theshared_ptritself, whereas theYparameter is a template parameter on that particularshared_ptrconstructor. Something like this:As for the example code you’ve posted, that looks fine to me.