The exact algorithm is specfic compiler. Here's how it works according to Itanium C ++ ABI (2.9.7) standard (written after and after GCC).
A pointer to the base class is a pointer to the middle of the body of the "large" class. The body of the "big" class is assembled in such a way that any base class that your pointer points to allows you to evenly access RTTI for this "big" class, which is actually your "base" class. This RTTI is a special structure that refers to the "big" information about the class: what type it has, what bases it has and at what displacements they are.
In fact, this is the "metadata" of the class, but in a more "binary" style.
V instance; Base *v = &instance; dynamic_cast<T>(v);
Dynamic listing takes advantage of the fact that when writing dynamic_cast<T>(v) compiler can immediately identify the metadata for the "large" class v - i.e. V ! When you write it, you think that T more output than Base , so the compiler will not be easy to throw with the base. But the compiler can immediately (at runtime) determine the most deuterated type - V - and it must then cross the inheritance graph contained in the metadata to see if it can disable V to T If possible, it just checks the offset. If it cannot or is single-valued, it returns NULL .
source share