I remember encountering this concept before, but can’t find it in Google now.
If I have an object of type A, which directly embeds an object of type B:
class A {
B b;
};
How can I have a smart pointer to B, e. g. boost::shared_ptr<B>, but use reference count of A? Assume an instance of A itself is heap-allocated I can safely get its shared count using, say, enable_shared_from_this.
D’oh!
Found it right in
shared_ptrdocumentation. It’s called aliasing (see section III of shared_ptr improvements for C++0x).I just needed to use a different constructor (or a corresponding
resetfunction overload):Which works like this (you need to construct shared_ptr to parent first):