本文介绍了在调试时将鼠标悬停在Visual Studio中的operator->()之后窥视字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上了一堂小课:-

class A{   public:int aField;   }

下面,在调试时,如果将鼠标悬停在a->aField中的aField上,Visual Studio将很好地弹出该字段的值(就像一个小的Watch).

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

然后我升级了代码以覆盖operator->:-

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

不再弹出. (a会弹出,而aField则不会弹出)
如何使可爱的弹出窗口再次出现?

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

编辑(赏金理由):"user1610015"提供了可行的解决方案,但我认为可能会有更方便的解决方案(只有一点点可以提供帮助).一个新的解决方案甚至可以向我建议一些插件或其他IDE.

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.


这是我想要的可爱弹出窗口的示例.
operator->之后的字段不起作用.

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

推荐答案

natvis可视化工具就是这种情况!幸运的是,您正在使用VS2015,该版本得到了完全支持.

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

以您为例

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

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

private:
    A* ptr;
};

您将需要创建一个扩展名为.natvis的文件,例如APtr.natvis,其内容如下:

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>

然后,您只需将此文件像其他任何.cpp文件一样添加到您的项目中,然后开始调试!

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

对于组成更复杂的内容,我强烈建议在C:\Program Files (x86)\Microsoft Visual Studio 14.0中找到内置的*.natvis文件,并以它们为例.

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.

这篇关于在调试时将鼠标悬停在Visual Studio中的operator->()之后窥视字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 23:24