+ -
当前位置:首页 → 问答吧 → 向下转换报错

向下转换报错

时间:2011-12-11

来源:互联网

bool operator==(IComponent &l , IComponent &r)
{
ComponentBase *cl = dynamic_cast<ComponentBase*>(&l);
ComponentBase *cr = dynamic_cast<ComponentBase*>(&r);
return cl->m_CompNo == cr->m_CompNo;
}
IComponent是ComponentBase的虚基类,这个函数能通过编译,但是运行时报:
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
错误,为什么?

作者: yusss   发布时间: 2011-12-11

引用楼主 yusss 的回复:
bool operator==(IComponent &amp;l , IComponent &amp;r)
{
ComponentBase *cl = dynamic_cast<ComponentBase*>(&amp;l);
ComponentBase *cr = dynamic_cast<ComponentBase*>(&amp;r);
return cl->m_CompNo == ……

或者这样写,也报错
bool operator==(IComponent &l , IComponent &r)
{
ComponentBase &cl = dynamic_cast<ComponentBase&>(l);
ComponentBase &cr = dynamic_cast<ComponentBase&>(r);
return cl.m_CompNo == cr.m_CompNo;
}

作者: yusss   发布时间: 2011-12-11