finding offset of subclasses

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Petar#M

    finding offset of subclasses

    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
  • Jack Klein

    #2
    Re: finding offset of subclasses

    On 9 Feb 2004 17:50:48 -0800, petar@scs.cs.ny u.edu (Petar#M) wrote in
    comp.lang.c++:
    [color=blue]
    > 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[/color]

    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.l earn.c-c++

    Comment

    • Pete Becker

      #3
      Re: finding offset of subclasses

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

      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)

      Comment

      • Victor Bazarov

        #4
        Re: finding offset of subclasses

        "Pete Becker" <petebecker@acm .org> wrote...[color=blue]
        > Jack Klein wrote:[color=green]
        > >
        > > If it's not a POD class, there is no standard C++ way to do this.
        > >[/color]
        >
        > 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.[/color]

        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


        Comment

        • Rob Williscroft

          #5
          Re: finding offset of subclasses

          Victor Bazarov wrote in news:f26Wb.2655 95$I06.2850867@ attbi_s01:
          [color=blue]
          > "Pete Becker" <petebecker@acm .org> wrote...[color=green]
          >> Jack Klein wrote:[color=darkred]
          >> >
          >> > If it's not a POD class, there is no standard C++ way to do this.
          >> >[/color]
          >>
          >> 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.[/color]
          >
          > 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?
          >[/color]

          yes,

          struct B_base { int bb; };

          struct B : virtual B_base
          {
          int data;
          };

          actual layout of b is say:

          struct __like_B
          {
          __implementatio n_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 */
          __implementatio n_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_bas e->bb;
          }

          Rob.
          --

          Comment

          • Pete Becker

            #6
            Re: finding offset of subclasses

            Victor Bazarov wrote:[color=blue]
            >
            > "Pete Becker" <petebecker@acm .org> wrote...[color=green]
            > > Jack Klein wrote:[color=darkred]
            > > >
            > > > If it's not a POD class, there is no standard C++ way to do this.
            > > >[/color]
            > >
            > > 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.[/color]
            >
            > 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?
            >[/color]

            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)

            Comment

            Working...