* Member offsets in multiple inheritance
@ 2011-05-26 17:52 pieniek
2011-05-26 18:02 ` Jonathan Wakely
2011-05-26 19:29 ` Sam Varshavchik
0 siblings, 2 replies; 3+ messages in thread
From: pieniek @ 2011-05-26 17:52 UTC (permalink / raw)
To: gcc-help
Hello,
I have always though, that given a pointer to an object, members are at
(this) + (member-offset). But this cannot be true for multiple
inheritance, no?
Example:
struct A {
int x,y;
}
struct B {
int a, b, c;
}
struct C : public A, public B {}
int func (B* b, B* actuallyCinstance) {
return b->b + actuallyCinstance->b;
}
How is the B::b member accessed, especially b->b when b is B? What is
the cost of that (x86_64)?
Thanks,
Edek
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Member offsets in multiple inheritance
2011-05-26 17:52 Member offsets in multiple inheritance pieniek
@ 2011-05-26 18:02 ` Jonathan Wakely
2011-05-26 19:29 ` Sam Varshavchik
1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2011-05-26 18:02 UTC (permalink / raw)
To: pieniek; +Cc: gcc-help
On 26 May 2011 12:27, pieniek <edek.pienkowski@gmail.com> wrote:
> Hello,
>
> I have always though, that given a pointer to an object, members are at
> (this) + (member-offset). But this cannot be true for multiple inheritance,
> no?
>
> Example:
>
> struct A {
> int x,y;
> }
>
> struct B {
> int a, b, c;
> }
>
> struct C : public A, public B {}
>
> int func (B* b, B* actuallyCinstance) {
> return b->b + actuallyCinstance->b;
> }
actuallyCinstance points to the B sub-object of a C instance, so the
offset to B::b is the same for both dereferences.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Member offsets in multiple inheritance
2011-05-26 17:52 Member offsets in multiple inheritance pieniek
2011-05-26 18:02 ` Jonathan Wakely
@ 2011-05-26 19:29 ` Sam Varshavchik
1 sibling, 0 replies; 3+ messages in thread
From: Sam Varshavchik @ 2011-05-26 19:29 UTC (permalink / raw)
To: gcc-help
[-- Attachment #1: Type: text/plain, Size: 824 bytes --]
pieniek writes:
> Hello,
>
> I have always though, that given a pointer to an object, members are at
> (this) + (member-offset). But this cannot be true for multiple inheritance,
> no?
>
> Example:
>
> struct A {
> int x,y;
> }
>
> struct B {
> int a, b, c;
> }
>
> struct C : public A, public B {}
>
> int func (B* b, B* actuallyCinstance) {
> return b->b + actuallyCinstance->b;
> }
>
> How is the B::b member accessed, especially b->b when b is B? What is the
> cost of that (x86_64)?
Both pointers received by func() are pointers to an instance of the B class.
The offset is the same.
If the caller invokes func() with the second argument being a pointer to an
instance of C, the caller adjust the pointer to point to C's superclass, and
invokes func() with two pointers to an instance of B.
[-- Attachment #2: Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2011-05-26 12:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-26 17:52 Member offsets in multiple inheritance pieniek
2011-05-26 18:02 ` Jonathan Wakely
2011-05-26 19:29 ` Sam Varshavchik
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).