This is the mail archive of the archer@sourceware.org mailing list for the Archer project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]