This is the mail archive of the
archer@sourceware.org
mailing list for the Archer project.
How to cast a pointer to a base class with multiple inheritance
- From: Matthieu VIAL <matthieu dot vial at orange-ftgroup dot com>
- To: archer at sourceware dot org
- Date: Fri, 31 Jul 2009 09:20:53 +0200
- Subject: How to cast a pointer to a base class with multiple inheritance
Hello,
I would like to know if it is possible to cast a pointer the way the
compiler do for classes with multiple inheritance with archer. The this
pointer changes depending on the base class. So how to find the other
pointers with the top pointer ?
The following code show the problem:
--------------------------
#include <iostream>
using namespace std;
class A
{
public:
A(int a);
int m_intA;
};
class B
{
public:
B(int b);
int m_intB;
};
class C : public A, B
{
public:
C(int c);
int m_intC;
};
A::A(int a)
{
cout << "ctor A :" << hex << this << endl;
m_intA = a;
}
B::B(int b)
{
cout << "ctor B :" << hex << this << endl;
m_intB = b;
}
C::C(int c) : A(c+1), B(c+2)
{
cout << "ctor C :" << hex << this << endl;
m_intC = c;
}
int main()
{
C c(1);
cout << "@C :" << hex << &c << endl;
cout << "@C as A:" << hex << (A*)&c << endl;
cout << "@C as B:" << hex << (B*)&c << endl;
return 0;
}
--------------------------
Output :
ctor A :0xbfd572d8
ctor B :0xbfd572dc
ctor C :0xbfd572d8
@C :0xbfd572d8
@C as A:0xbfd572d8
@C as B:0xbfd572dc
--------------------------
Thanks,
Matthieu VIAL