Multiple inheritance

J

junw2000

Below is a simple code about multiple inheritance

#include <iostream>

class A{
int x;
};

class B{
int y;
};

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

int main(){
A * a;
B* b;
C c;

a = &c;
b = &c;
std::cout<<"a:"<<a<<" b:"<<b<<" &c:"<<&c<<std::endl;
}

The output is:

a:0xbffff540 b:0xbffff544 &c:0xbffff540

Why a and b are different?

Thanks.

Jack
 
V

Victor Bazarov

Below is a simple code about multiple inheritance

#include <iostream>

class A{
int x;
};

class B{
int y;
};

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

int main(){
A * a;
B* b;
C c;

a = &c;
b = &c;
std::cout<<"a:"<<a<<" b:"<<b<<" &c:"<<&c<<std::endl;
}

The output is:

a:0xbffff540 b:0xbffff544 &c:0xbffff540

Why a and b are different?

Conversion of the pointer to 'c' to a pointer to 'a' causes
the adjustment of the address to make the pointer point to the
subobject inside the 'c' object.

Two subobjects of the same object cannot have the same address
because you wouldn't be able to tell between them.

V
 
T

Thomas Tutone

Below is a simple code about multiple inheritance

#include <iostream>

class A{
int x;
};

class B{
int y;
};

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

int main(){
A * a;
B* b;
C c;

a = &c;
b = &c;
std::cout<<"a:"<<a<<" b:"<<b<<" &c:"<<&c<<std::endl;
}

The output is:

a:0xbffff540 b:0xbffff544 &c:0xbffff540

Why a and b are different?

Because a points to the "A" portion of c, and b points to the "B"
portion of c. In other words, a likely points directly to c.x, while b
likely points directly to c.y. You could modify your program slightly
to test that hypothesis.

Best regards,

Tom
 
L

Lyell Haynes

Below is a simple code about multiple inheritance

#include <iostream>

class A{
int x;
};

class B{
int y;
};

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

int main(){
A * a;
B* b;
C c;

a = &c;
b = &c;
std::cout<<"a:"<<a<<" b:"<<b<<" &c:"<<&c<<std::endl;
}

The output is:

a:0xbffff540 b:0xbffff544 &c:0xbffff540

Why a and b are different?

You should try to layout what the classes look like in memory to see it
more clearly:

here's a simplified view:

A -> [int - x]
B -> [int - y]
C -> [int - x | int - y | int - z]

As you can see, C contains the members of both A and B, so a pointer to
a C object would point to the beginning of all the data, i.e. "x".

If I create a pointer of type A and have it point to an object of type
C, since the A object contains "x", it will also point to that
location, the same as the C object.

If I create a pointer of type B and have it point to an object of type
C, since the B object only contains "y", it will need to point to the
"y" location in the C object, so that it correctly looks like a B
object.

In your example, you see that the B pointer is 4 bytes greater than the
A pointer (the size of an int on a 32 bit platform) which fits
perfectly with my example above.
 

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,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top