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]

Re: How to cast a pointer to a base class with multiple inheritance


> Matthieu> I would like to know if it is possible to cast a pointer the
> Matthieu> way the compiler do for classes with multiple inheritance with
> Matthieu> archer. The this pointer changes depending on the base
> Matthieu> class. So how to find the other pointers with the top pointer
> Matthieu> ?
> 
> Tom> I think Value.cast should do the right thing here.  I haven't tried this
> Tom> myself, though... did you try it?  It wasn't clear from your message.
> 

Thank for your answer.

Sorry Value.cast seems to work but my problem is actually a bit more
complex.

I have many classes which implement an interface and inheritate a base
class.

I need to compare pointers to the interface with pointers to the base
class. But these classes are not directly related so Value.cast is
useless. The solution is to up cast the base class pointer to the final
class and then down cast to the interface. But i need run time type
information for that. The typeid operator doesn't seem to be available
in archer.

Here is a new code sample

#include <iostream>

using namespace std;

class BaseClass
{
public:
    BaseClass(int a);

    int m_intA;
};

class Interface
{
public:
    virtual void Method() = 0;
};

class Class1 : public Interface, public BaseClass
{
public:
    Class1(int c);
    
    virtual void Method();

    int m_intC;
};

class Class2 : public Interface, public BaseClass
{
public:
    Class2(int c);

    virtual void Method();

    int m_intC;
};


BaseClass::BaseClass(int a)
{
    cout << "ctor BaseClass :" << hex << this << endl;
    m_intA = a;
}

Class1::Class1(int c) : BaseClass(c+1)
{
    cout << "ctor Class1 :" << hex << this << endl;
    m_intC = c;
}

void Class1::Method()
{
}

Class2::Class2(int c) : BaseClass(c+2)
{
    cout << "ctor Class2 :" << hex << this << endl;
    m_intC = c;
}

void Class2::Method()
{
}


int main()
{
    Class1 c1(1);
    Class2 c2(1);

    Interface* pIc1 = &c1;
    Interface* pIc2 = &c2;
    BaseClass* pBc1 = &c1;
    BaseClass* pBc2 = &c2;

    cout << "@C1 :" << hex << &c1 << endl;
    cout << "@C1 as Interface :" << hex << pIc1 << endl;
    cout << "@C1 as BaseClass :" << hex << pBc1 << endl;
    cout << "@C2 :" << hex << &c2 << endl;
    cout << "@C2 as Interface :" << hex << pIc2 << endl;
    cout << "@C2 as BaseClass :" << hex << pBc2 << endl;

    return 0;
}


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