从函数返回后丢失 RTTI 信息
给定一个类和子类:
class Event {...}
class Note : public Event {...}
注释被克隆并存储在函数 f() 内的指针中。类型信息保留在指针中,可以通过dynamic_cast恢复:
void f()
{
pEvent = pNote->Clone(); // create a clone of a Note
ASSERT(dynamic_cast<Note*>(pEvent)); // check the pointer, here it works
}
现在,从f()返回后,类型信息丢失:
f();
ASSERT(dynamic_cast<Note*>(pEvent)); // -> "Access violation - no RTTI-data"
VS调试器显示有效的指针值(未更改),但不显示派生类, 除了处于 f()
范围内时之外。
从函数返回时,指针的 RTTI 信息如何丢失?
Given a class and subclass:
class Event {...}
class Note : public Event {...}
A Note is Cloned and stored in a pointer within a function f(). The type-information is preserved in the pointer and can be recovered by dynamic_cast:
void f()
{
pEvent = pNote->Clone(); // create a clone of a Note
ASSERT(dynamic_cast<Note*>(pEvent)); // check the pointer, here it works
}
Now, after returning from f() the type-information is lost:
f();
ASSERT(dynamic_cast<Note*>(pEvent)); // -> "Access violation - no RTTI-data"
The VS-debugger shows a valid pointer-value (unchanged), but not the derived class,
other than while beeing in the f()
-scope.
How can the RTTI-info for a pointer be lost when returning from a function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个析构函数不小心损坏了指针。消除此错误后,RTTI 将按预期工作。
There was a destructor accidently doing harm to the pointer. After removing this error, the RTTI works as expected.