This has nothing to do with std::enable_shared_from_this
; it's the general scenario of multiple inheritance. Inside a member function of class X
, the type of this
is X*
, i.e. it points to the X
subobject of the complete object. So when calling A::fa()
on a B
object, this
refers to the A
subobject of B
. That can be offset from the object's initial address, based on the compiler's chosen memory layout.
In other words, an object B
contains both a base class object A
and a base class object S<B>
. Since you listed S<B>
first, the compiler chose to lay out S<B>
first in B
's layout, and A
after it. So the memory layout looks something like this:
+-------+-----+---------------------------+
| S<B> | A | B's non-inherited members |
+-------+-----+---------------------------+