If we had this code:
class Base
{
int anint;
float afloat;
};
class Derived : Base
{
//inherited member variables...
};
I have been told that members of Base
would be inherited to Derived
and these the inherited members in Derived
are actually inside a base class subobject of Base
(but this subobject is unnamed); a subobject of Base
is created in Derived that holds the members that are inherited. So when accessing a member in a class, there is an implicit invocation to the this pointer, unless you do something explicitly, but is there also an implicit pointer (or anything) invoked when accessing an inherited object? Like, if we accessed anint
in an instance of Derived
by derivedInstance->anint
, would this actually look like derivedInstance->this->somethingToBaseObjectThatHoldsTheMembers->anint
or how does this work?
No. The compiler choses a layout (ABI). Static casts leverage knowledge of that layout to adjust pointers using
static_cast
.RTTI enables dynamic pointer adjustments using
dynamic_cast
.See e.g. Regular cast vs. static_cast vs. dynamic_cast