Short answer: the compiler will adjust the values ββof the pointer during cast operations if it knows the relationship between the base and derived classes.
Let's say the address of your instance of an object of class C was at address 100. And let sayofof (C) == 4. Like sizeof (B) and sizeof (A).
When a throw occurs, for example:
C c; A* pA = &c;
The pointer value pA will be the memory address c plus the offset from where "A" begins with C. In this case, pA will refer to the memory address 104 if sizeof (B) is also 4.
All this is true for passing a pointer to a derived class into a function that expects a pointer to a base class. Implicit casting will occur in the same way as pointer offset adjustment.
Similarly, for downcasting:
C* pC = (C*)(&a);
The compiler will take care of setting the pointer value during setup.
The one who βreceivedβ all this when the class is declared ahead without a full declaration:
This is a real mistake!
My general rule. Avoid the old "C-style" and prefer using the static_cast operator or just rely on implicit casting without an operator to do the right thing (to enhance). The compiler will throw an error if the casting is invalid.
source share