我可以在C ++结构上实现.ToString()以便进行调试吗? [英] Can I implement .ToString() on C++ structs for debugging purposes?

查看:235
本文介绍了我可以在C ++结构上实现.ToString()以便进行调试吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,如果我定义一个结构,我也可以重写ToString().然后,当我调试并添加手表或将鼠标悬停在该结构的实例上时,工具提示将是计算出的ToString()而不是该结构的类型名称.

In C#, if I define a struct, I can also override ToString(). Then when I'm debugging and I add a watch or hover my mouse over an instance of the struct, the tooltip will be the computed ToString() rather than the type name of the struct.

我可以在C ++和/或C ++/CLI中做到这一点吗?也就是说,我是否可以将方法定义为struct的一部分(或执行其他任何操作),这将导致watch-value/tooltip显示我选择的字符串? Visual Studio for C/C ++中的默认字符串呈现是所有结构的字段值的列表(或可放入小框的字段值).

Can I do that in C++ and/or C++/CLI somehow? That is, can I define a method as part of the struct (or do anything else) that will cause the watch-value/tooltip to display a string of my choosing? The default string rendering in Visual Studio for C/C++ is a list of all the struct's field values (or as many as can be jammed into the little box).

我键入所有C样式的结构. (它实际上是用C编写的,然后再将文件转换为.cpp并修复了一些类型问题,以便可以在CLI中运行它.)这是一个示例struct:

My types all C-style structs. (It was actually written in C before I converted the files to .cpp and fixed some type issues so I could run it in CLI.) Here's an example struct:

struct other_dollars_node
{
    struct other_dollars_node *next_other_dollars;
    override *overrides;    
    long     other_dollars_id;
    tm       effective_date;
    double   amount;
}

我对C ++/CLI的经验很少-我的大部分经验是对本机C/C ++和C#的了解.我正在使用Visual Studio 2013.

I have very little experience with C++/CLI -- most of my experience has been with native C/C++ and C#. I'm using Visual Studio 2013.

更新:由于几乎所有现有代码都使用本机C语法,并且我希望无需重构即可使用的解决方案,因此CLI方面可能不太重要.

推荐答案

我认为您要做的是为本机结构提供调试器可视化.我在MSDN上进行了一些搜索,并找到了以下页面:创建本机对象的自定义视图在调试器中.

I think what you want to do is to provide a debugger visualization for your native structs. I did a little searching on MSDN, and found this page: Create custom views of native objects in the debugger.

基本上,您需要做的是向C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Packages\Debugger\Visualizers添加一个文件,以告诉Visual Studio如何在调试器窗口中显示结构.那里有很多示例,并且上面的链接提供了一些很好的解释,尽管我承认我自己还没有尝试过.

Basically, what you need to do is add a file to C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Packages\Debugger\Visualizers to tell Visual Studio how to show your structs in the debugger window. There are many examples there, and the link above provides some good explanation, though I admit I haven't tried it myself.

这篇关于我可以在C ++结构上实现.ToString()以便进行调试吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆