The compiler does not need to know anything about the size of the object it is dereferencing. All dereferencing does is load the value at the address contained in our pointer.
When refers to a member of a struct or class with the dot (.) operator, the compiler fetches the value at the designated offset from the beginning of the struct or class.
When one uses the arrow (->) operator, the compiler fetches the address and returns the offset to the element requested.
When one increments a pointer, the compiler adds the sizeof
the object to the pointer value in memory, since the array of objects is contiguous, we are now pointing to the next object.
In C++, the element of the base classes are stored at before the data of the derived class, so downcasting or upcasting does not affect the pointer value.
In the case of mulitple inheritance, casting to a base class makes the compiler return the offset of the base class object in the derived class. This is why static_cast
and reinterpret_cast
may return different values.