mouse-over to peek a field after operator->() in Visual Studio while debugging

非 Y 不嫁゛ 提交于 2019-12-04 01:57:55

问题


I had a tiny class:-

class A{   public:int aField;   }

Below, while debugging, if I hover mouse around aField in a->aField, Visual Studio will pop up the value of the field nicely (like a tiny Watch).

A* a=new A();
a->aField=1234;   
    //^ hover here

Then I upgraded code to override operator->:-

class APtr{ //my custom smart pointer
    A* ptr;
    A* operator->(){ return ptr; }
}

APtr a; 
.....
a->aField=1234;
   //^ hover here

There is no pop up anymore. (There is a popup for a, but not for aField)
How to make the cute popup appear again?

Edit (Bounty reason): "user1610015" has provided a doable solution, but I think there might be a more convenient solution (only a little can help). A new solution can even suggest me to some plugins or other IDEs.

Edit 2:
Here is an example of the cute popup that I want.
It doesn't work for the field after operator->.


回答1:


That's exactly the case for natvis visualizers! Luckily, you're using VS2015, where they're fully supported.

For your example

class APtr
{
public:
    APtr(A* a_Pointer)
    {
        ptr = a_Pointer;
    }

    A* operator->()
    {
        return ptr;
    }

private:
    A* ptr;
};

You will need to create a file with .natvis extension, for example APtr.natvis, with the following content:

<?xml version="1.0" encoding="utf-8"?>

<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
  <Type Name="APtr">
      <SmartPointer Usage="Minimal">ptr</SmartPointer>
  </Type>
</AutoVisualizer>

Then you simply add this file to your project, like any other .cpp file, and start debugging!

For composing anything more complicated, I highly recommend finding built-in *.natvis files in C:\Program Files (x86)\Microsoft Visual Studio 14.0 and using them as example.




回答2:


The debugger doesn't recognize the operator->, but you can get the same information by hovering over the smart pointer variable and then the right arrow on the tooltip:



来源:https://stackoverflow.com/questions/39339959/mouse-over-to-peek-a-field-after-operator-in-visual-studio-while-debugging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!