何时和为什么会产生std :: __ non_rtti_object异常? [英] When and why is an std::__non_rtti_object exception generated?

查看:1050
本文介绍了何时和为什么会产生std :: __ non_rtti_object异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio并执行有效的动态投射。 RTTI已启用。

I'm using Visual Studio and performing a valid dynamic cast. RTTI is enabled.

编辑:将代码更新为更现实

Edit : Updated the code to be more realistic

struct base
{
    virtual base* Clone()
    {
        base* ptr = new base;
        CopyValuesTo( ptr );
        return ptr;
    }
    virtual void CopyValuesTo( base* ptr )
    {
       ...
    }
    virtual ~base()
    {
    }
}

struct derived : public base
{
    virtual base* Clone()
    {
        derived* ptr = new derived;
        CopyValuesTo( ptr );
        return ptr;
    }
    virtual void CopyValuesTo( base* ptr )
    {
       ...
    }
    virtual ~derived()
    {
    }
}  

void Class1::UseNewSpec( base* in_ptr ) //part of a totally unrelated class
{
    derived* ptr = dynamic_cast<derived *>(in_ptr);
    if( !ptr )
       return;
    delete m_ptr;
    m_ptr = ptr->Clone(); //m_ptr is a member of Class1 of type base*
}

//usage : 
Class1 obj;
derived new_spec; 
obj.UseNewSpec( &new_spec );

我的调试器说,当抛出异常时,in_ptr是正确的类型。谷歌似乎特别无益。有任何想法吗?干杯。

My debugger says that in_ptr is of the correct type when the exception is thrown. Google seems particularly unhelpful. Any ideas? Cheers.

推荐答案

我根据你的伪代码运行测试,它工作。因此,如果RTTI在您的构建配置中真正启用,那么它必须是未发布的另一个问题。

I ran a test based on your pseudo-code and it works. So if RTTI is truly enabled in your build configuration, then it must be another problem that isn't captured in what you posted.

这篇关于何时和为什么会产生std :: __ non_rtti_object异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆