问题描述
最近我从客户那里得到了一个崩溃转储文件。我可以将问题跟踪到一个可能包含不正确数据的类,但是我只得到一个void指针,而不是一个真正的指针(void-pointer来自一个窗口属性,因此它是一个void-pointer) 。不幸的是,我想把这个指针的类放在一个匿名的命名空间里,就像这样:
命名空间
{
class MyClass
{
...
};
}
...
void * ptr = ...
//我知道ptr指向MyClass的一个实例,
//和在这个位置,我想在调试器中将ptr转换为(MyClass *)。
如果Visual Studio 2005只显示指针值,则在监视窗口中使用ptr
如果我使用(MyClass *)ptr,调试器告诉我不能转换它。
如何将ptr转换为MyClass - 指针?
注意:我最终可以使用一个愚蠢的命名空间(如源文件的名称),然后使用using namespace,但我会期待更好的解决方案。
这是在。看起来并不是一个很好的解决方案(你可以从我的列表中获得你的课程的装饰名称)。
你的命名空间的想法可以正常工作,你也可以用一个愚蠢的名字声明一个相同的类,而不是转换为该类型。
Recently, I got a crash dump file from a customer. I could track the problem down to a class that could contain incorrect data, but I only got a void-pointer to the class, not a real pointer (void-pointer came from a window-property, therefore it was a void-pointer).Unfortunately, the class to which I wanted to cast the pointer to, was in an anonymous namespace, like this:
namespace
{
class MyClass
{
...
};
}
...
void *ptr = ...
// I know ptr points to an instance of MyClass,
// and at this location I want to cast ptr to (MyClass *) in the debugger.
Using "ptr" in the watch window if Visual Studio 2005 just shows the pointer value.If I use "(MyClass *)ptr", the debugger tells me it cannot cast to it.
How can I cast the ptr to a MyClass-pointer?
Note: I could eventually use a silly-named namespace (like the name of the source file), and then use a "using namespace", but I would expect better solutions.
This is mentioned in MSDN. It doesn't look like there's a nice solution within the Watch window (you can get the decorated name of your class from a listing I guess).
Your "silly-named namespace" idea would work okay, you could also just declare an identical class with a silly name and cast to that type instead.
这篇关于在任意命名空间中调试数据(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!