Reputation: 23619
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.
When I use ptr
in the watch window, 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 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.
Upvotes: 14
Views: 5250
Reputation: 508
Referencing anonymous namespaces in Visual Studio Debugger's expressions is not supported (at least as of VS 2017) and it's really annoying.
From https://learn.microsoft.com/en-us/visualstudio/debugger/expressions-in-the-debugger#c-expressions
Anonymous namespaces are not supported. If you have the following code, you cannot add test to the watch window:
namespace mars { namespace { int test = 0; } } int main() { // Adding a watch on test does not work. mars::test++; return 0; }
Upvotes: 3
Reputation: 1763
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.
Upvotes: 10