Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Up casting and down casting

Reply
Thread Tools

Up casting and down casting

 
 
Sosuke
Guest
Posts: n/a
 
      12-20-2009
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.
 
Reply With Quote
 
 
 
 
Michael Tsang
Guest
Posts: n/a
 
      12-20-2009
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sosuke wrote:

> 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-----

 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      12-20-2009
On 20 Dec, 11:13, Michael Tsang <(E-Mail Removed)> wrote:
> Sosuke wrote:
> > 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.


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.

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
Does Casting Slow a Program Down? PeterOut C Programming 23 02-04-2007 05:44 PM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM
casting my smart pointer up and down Ingo Nolden C++ 5 12-15-2004 08:53 AM
Why down-casting is done here? mead C++ 7 05-03-2004 12:57 AM



Advertisments