If we use multiple inheritance, slicing will make the addresses to parent objects differ from the address to leaf objects:
struct X {int x};
struct Y {int y};
struct Z : X, Y {int z};
So if we have a Z
object z
, its address &z
will not coincide with the address of its Y
parent: static_cast<Y*>(&z)
is four bytes higher than &z
.
The good thing about static_cast
is that it's, well, static, so doesn't take up runtime (compared to dynamic_cast
, that is).
However, if we have a Z*
that points at 0
, every cast to a parent should and does yield a null pointer as well.
Why does this work and how is it implemented? Does that imply that every single static_cast
introduces a branch instruction?