A deep dive into a subtle C++ bug caused by misusing reinterpret_cast when converting derived class pointers to integral addresses. The post demonstrates how multiple inheritance and virtual functions can shift the memory layout of objects, causing reinterpret_cast to read the wrong member. The fix is to first use static_cast to the target base type before applying reinterpret_cast, allowing the compiler to correctly adjust the pointer offset. The post concludes with guidance on when to use each cast type and why C++ casts are intentionally verbose.

6m read timeFrom galowicz.de
Post cover image
Table of contents
Casting done rightSummary

Sort: