finding offset of subclasses

P

Petar#M

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
 
J

Jack Klein

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

Pete Becker

Jack said:
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.
 
V

Victor Bazarov

Pete Becker said:
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
 
R

Rob Williscroft

Victor Bazarov wrote in
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.
 
P

Pete Becker

Victor said:
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.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top