Up casting and down casting

S

Sosuke

How is up casting and down casting performed? Maybe it's an
implementation detail, but hopefully it's something that most
compilers implement in the same way. My first guess was that the
compiler places the base class at the beginning of the derived class,
so that both have the same address. But that wouldn't work in the case
of multiple inheritance.
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
How is up casting and down casting performed? Maybe it's an
implementation detail, but hopefully it's something that most
compilers implement in the same way. My first guess was that the
compiler places the base class at the beginning of the derived class,
so that both have the same address. But that wouldn't work in the case
of multiple inheritance.

The cast adds/subtracts the offset of the derived class sub-object inside
the base class object.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAksuBtoACgkQG6NzcAXitM/FEACfdDvoGrn+pISyZoSOW0SXbyiC
QVwAn3PM3XuV9HMSwez+RADvpTQu+ME3
=xDj/
-----END PGP SIGNATURE-----
 
J

James Kanze

The cast adds/subtracts the offset of the derived class
sub-object inside the base class object.

That doesn't work if virtual inheritance is involved. In the
case of virtual inheritance, the compiler has to generate code
to look up the amount to add or subtract, generally using some
sort of information in the vtable. And the details for this
vary enormously from one compiler to the next.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,836
Latest member
BuyBlissBitesCBD

Latest Threads

Top