gpt4 book ai didi

c++ - 何时以及为何会生成 std​​::__non_rtti_object 异常?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:57:09 28 4
gpt4 key购买 nike

我正在使用 Visual Studio 并执行有效的动态转换。启用 RTTI。

编辑:更新代码使其更真实

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 是正确的类型。谷歌似乎特别没有帮助。有任何想法吗?干杯。

最佳答案

https://learn.microsoft.com/en-us/cpp/cpp/typeid-operator?view=vs-2019有关于 __non_rtti_object_exception 的信息。

来自 MSDN:

If the pointer does not point to a valid object, a __non_rtti_objectexception is thrown, indicating an attempt to analyze the RTTI that triggered a fault (like access violation), because the object is somehow invalid (bad pointer or the code wasn't compiled with /GR).

关于c++ - 何时以及为何会生成 std​​::__non_rtti_object 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/854864/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com