【发布时间】:2013-11-18 09:17:53
【问题描述】:
我想将对象 hlaObj 从 RtiValueAggregate 转换为 HlaObject 但它不起作用......!两个类之间没有继承。而 HlaObject 实际上是从 RtiValue 派生的。 谁能告诉我以下代码有什么问题: 谢谢!
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;
}
【问题讨论】:
-
it's not working。查看错误消息,它们应该会告诉您出了什么问题。 -
您使用的是什么构建器?我用
g++构建它并且没有错误被转储。另一方面,您是否在另一部分代码中使用了多重继承?否则,我认为没有必要使用虚拟继承。
标签: c++ casting virtual virtual-inheritance typecasting-operator