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

前端 未结 2 1399
春和景丽
春和景丽 2021-01-14 20:17

I had a tiny class:-

class A{   public:int aField;   }

Below, while debugging, if I hover mouse around aField in a->a

相关标签:
2条回答
  • 2021-01-14 20:45

    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.

    0 讨论(0)
  • 2021-01-14 20:52

    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:

    0 讨论(0)
提交回复
热议问题