finding offset of subclasses

Discussion in 'C++' started by Petar#M, Feb 10, 2004.

  1. Petar#M

    Petar#M Guest

    Dear all,

    Is there any way to derive the offset of a subclass without instantiating
    the class (in other words none of the *_cast operators can be used).

    So, e.g.:

    class A {};
    class B {};
    class C : public A, public B {};

    I want to know the offset of B within C.

    Thanks,
    Petar
    Petar#M, Feb 10, 2004
    #1
    1. Advertising

  2. Petar#M

    Jack Klein Guest

    On 9 Feb 2004 17:50:48 -0800, (Petar#M) wrote in
    comp.lang.c++:

    > Dear all,
    >
    > Is there any way to derive the offset of a subclass without instantiating
    > the class (in other words none of the *_cast operators can be used).
    >
    > So, e.g.:
    >
    > class A {};
    > class B {};
    > class C : public A, public B {};
    >
    > I want to know the offset of B within C.
    >
    > Thanks,
    > Petar


    Why do you want to know this? Perhaps you need to rethink your
    design.

    In any case, if the class is a POD class, which means respectively
    that all its base classes are POD classes, the standard offsetof()
    macro from <stddef.h> or <cstddef> can be used.

    If it's not a POD class, there is no standard C++ way to do this.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Feb 10, 2004
    #2
    1. Advertising

  3. Petar#M

    Pete Becker Guest

    Jack Klein wrote:
    >
    > If it's not a POD class, there is no standard C++ way to do this.
    >


    And there may not be a single value: virtual bases can be at different
    offsets in the same derived type, depending on the most derived type.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Feb 10, 2004
    #3
  4. "Pete Becker" <> wrote...
    > Jack Klein wrote:
    > >
    > > If it's not a POD class, there is no standard C++ way to do this.
    > >

    >
    > And there may not be a single value: virtual bases can be at different
    > offsets in the same derived type, depending on the most derived type.


    I am not sure I understand that. So, if 'B' is a stand-alone object,
    it can have a different layout than if it's a sub-object of another
    object, when virtual inheritance is involved?

    Thanks.

    Victor
    Victor Bazarov, Feb 10, 2004
    #4
  5. Victor Bazarov wrote in news:f26Wb.265595$I06.2850867@attbi_s01:

    > "Pete Becker" <> wrote...
    >> Jack Klein wrote:
    >> >
    >> > If it's not a POD class, there is no standard C++ way to do this.
    >> >

    >>
    >> And there may not be a single value: virtual bases can be at different
    >> offsets in the same derived type, depending on the most derived type.

    >
    > I am not sure I understand that. So, if 'B' is a stand-alone object,
    > it can have a different layout than if it's a sub-object of another
    > object, when virtual inheritance is involved?
    >


    yes,

    struct B_base { int bb; };

    struct B : virtual B_base
    {
    int data;
    };

    actual layout of b is say:

    struct __like_B
    {
    __implementation_defined __ptr_to_B_base;
    int data;
    B_base __actual_B_base; /* actual subobject */
    };

    struct C: B_base, B
    {
    };

    actual layout of C:

    struct __like_C
    {
    B_base __actual_B_base; /* C & B subobject */
    __implementation_defined __ptr_to_B_base; /* B */
    int data; /* B */
    };

    __ptr_to_B_base need to be initialized by B or C's ctor.

    int get_bb( B *bp )
    {
    return bp->bb;

    gets translated to something like:

    return bp->__ptr_to_B_base->bb;
    }

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Feb 10, 2004
    #5
  6. Petar#M

    Pete Becker Guest

    Victor Bazarov wrote:
    >
    > "Pete Becker" <> wrote...
    > > Jack Klein wrote:
    > > >
    > > > If it's not a POD class, there is no standard C++ way to do this.
    > > >

    > >
    > > And there may not be a single value: virtual bases can be at different
    > > offsets in the same derived type, depending on the most derived type.

    >
    > I am not sure I understand that. So, if 'B' is a stand-alone object,
    > it can have a different layout than if it's a sub-object of another
    > object, when virtual inheritance is involved?
    >


    Yup. Let's look at two classes, B and C, each with a virtual base A:

    +--------+--------+
    | B data | A data |
    +--------+--------+

    +--------+--------+
    | C data | A data |
    +--------+--------+

    Now derived a class D from both B and C. One possible layout is:

    +--------+--------+--------+--------+
    | D data | B data | A data | C data |
    +--------+--------+--------+--------+

    Another is:

    +--------+--------+--------+--------+
    | D data | B data | C data | A data |
    +--------+--------+--------+--------+

    In the first, the offset of the A subobject from the C subobject is
    different from its offset in a standalone C object. In the second it's
    the offset from the B subobject that's different.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Feb 10, 2004
    #6
    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. Lance Riedel

    Translated Offset to Source Offset

    Lance Riedel, Oct 14, 2003, in forum: XML
    Replies:
    2
    Views:
    499
    Patrick TJ McPhee
    Oct 15, 2003
  2. Rahul
    Replies:
    19
    Views:
    626
    Serge Paccalin
    Nov 15, 2006
  3. Osmo Maatta
    Replies:
    3
    Views:
    478
    Osmo Maatta
    Aug 29, 2010
  4. Dorren

    about finding subclasses

    Dorren, Apr 15, 2007, in forum: Ruby
    Replies:
    5
    Views:
    118
    ChrisKaelin
    Apr 16, 2007
  5. Roy Smith
    Replies:
    4
    Views:
    266
    Roy Smith
    Jan 27, 2013
Loading...

Share This Page