0

I want to cast object hlaObj to HlaObject from RtiValueAggregate but it's not working...! There is no inheritance between two classes. and HlaObject is virtually derived from RtiValue. can anyone tell me whats wrong with the following code: Thanks!

class RtiValue;
class HlaObject;

class RtiValueAggregate 
{
public:
    friend class RtiValue;
    int w;

    RtiValueAggregate() : w(10)
    {        
    }

};


class RtiValue
{
public:
    friend class RtiValueAggregate;

    RtiValue()
    {
        int x = 5;
        pAggregate_ = new RtiValueAggregate();
    }

    RtiValueAggregate* getAggregate() const
    {
        return pAggregate_;
    }

private:
    RtiValueAggregate* pAggregate_;
};


class ObjectAttribute : public RtiValue
{
public:
    int v;

    ObjectAttribute() : v(0)
    {
        RtiValue();
    }        

};


class HlaObject :public virtual RtiValueAggregate
{
public:
    int a;
    ObjectAttribute* ptrV;

    HlaObject() : a(1)
    {
        ptrV = new ObjectAttribute();
    }

};


int _tmain(int argc, _TCHAR* argv[])
{
    RtiValue *rtiVal = new RtiValue();
    RtiValueAggregate* rtiValueAggr = rtiVal->getAggregate();
    HlaObject *hlaObj = reinterpret_cast<HlaObject*>(rtiValueAggr);

    cout << "Press any key to exit..." << endl;
    cin.get();
    return 0;
}
Hulk
  • 6,399
  • 1
  • 30
  • 52
mnemonic
  • 41
  • 1
  • 2
  • `it's not working`. Look at the error messages, they should tell you what's wrong. – n. m. could be an AI Nov 18 '13 at 09:24
  • What builder are you using? I built it with `g++` and no error is dumped. On the other hand, do you use multiple inheritance in another part of the code? Otherwise, I don't see the need of using virtual inheritance. – jcm Nov 18 '13 at 09:47

1 Answers1

0

Use dynamic_cast, never use reinterpret_cast.

BTW, why are doing:

class HlaObject :public virtual RtiValueAggregate

And not:

class HlaObject :public RtiValueAggregate
Johan
  • 3,728
  • 16
  • 25