Up casting and down casting

Discussion in 'C++' started by Sosuke, Dec 20, 2009.

  1. Sosuke

    Sosuke Guest

    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.
     
    Sosuke, Dec 20, 2009
    #1
    1. Advertising

  2. -----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-----
     
    Michael Tsang, Dec 20, 2009
    #2
    1. Advertising

  3. Sosuke

    James Kanze Guest

    On 20 Dec, 11:13, Michael Tsang <> 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
     
    James Kanze, Dec 20, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevin
    Replies:
    11
    Views:
    5,811
    Andrew McDonagh
    Jan 8, 2005
  2. mead
    Replies:
    7
    Views:
    373
    Leor Zolman
    May 3, 2004
  3. Ingo Nolden
    Replies:
    5
    Views:
    3,891
    Siemel Naran
    Dec 15, 2004
  4. PeterOut

    Does Casting Slow a Program Down?

    PeterOut, Jan 31, 2007, in forum: C Programming
    Replies:
    23
    Views:
    698
    Barry Schwarz
    Feb 4, 2007
  5. Wally Barnes
    Replies:
    3
    Views:
    530
    Wally Barnes
    Nov 20, 2008
Loading...

Share This Page