Re: Casting pointer to derived class and vice versa
On Apr 24, 7:27 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
sk_usenet wrote:
"Taras_96" <taras...@gmail.com> wrote in message > Hi everyone,
accordingly. reinterpret_cast is just a brute cast.
static_cast would make the derived class pointer point to the
appropriate base class (as laid out in memory), hence you see
different values.
"Hence"? There is no 'static_cast' required to convert from the
derived class to the accessible unambiguous base class. And the
different values are only because the sizes of the base class
subobjects are not 0, so they occupy some space in the derived
class object.
Just a nit but, while your statement is generally true in
practice, the standard doesn't require it. The standard gives
the compiler pretty much unlimited leeway in how it lays things
out when inheritance is involved, and even in the simplest
cases, there's no guarantee that the address of the base class
is the same as the address of the derived class.
Using 'reinterpret_cast' in this situation is simply not
legal.
The reinterpret_cast itself is legal. Doing anything with the
results of it isn't (except for casting it back to the original
type).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34