Can I implement .ToString () in C ++ structures for debugging purposes?

In C #, if I define a structure, I can also override ToString (). Then, when I debug and add a clock or hover over an instance of the structure, the tooltip will be computed by ToString (), not the type of the structure name.

Can I do this in C ++ and / or C ++ / CLI? That is, can I define the method as part of the structure (or do something else), which will cause watch-value / tooltip to display a string of my choice? The default string rendering in Visual Studio for C / C ++ is a list of all the structure field values ​​(or as many as can be clamped in a small box).

My types are all C-style structures. (In fact, it was written in C before I converted the files to .cpp and fixed some type problems so that I could run it in the 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; } 

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

Update: since almost all existing code uses native C syntax, and I would prefer a solution that works without the need for refactoring, the CLI aspect may be less important.

+6
source share
1 answer

I think you want to do a debugger visualization for your own structures. I searched the MSDN a bit and found this page: Create my own views of my own objects in the debugger .

Basically, you need to add the file to C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Packages\Debugger\Visualizers to tell Visual Studio how to show your structures in the debugger window. There are many examples there, and the link above gives some good explanation, although I admit that I have not tried this myself.

+4
source

Source: https://habr.com/ru/post/977302/


All Articles