Phil-ZXX
Phil-ZXX

Reputation: 3265

Structuring the Details of a C++ Class in Debug

How does one enhance the debug information shown for a C++ Class? Specifically, let's look at the vector class: its top level view only contains its size variable (#1 in image) and the expanded view shows its elements one-by-one as parameters so to speak (#2 in image).

How does one control what's being shown in the top-level summary, and what's being shown in the sub-level? Are there certain design patterns that achieve this?

Edit: Is there a cross-platform solution? One that works for CLion under Linux, VS on Win, CMake in general, etc.

A small working example that replicates the vector behaviour would be incredibly useful (if that's even possible without having to replicate the complex nature of the actual vector class).

screenshot


Say, I attempt to write a class as follows

template <class T> class Vector
{
public:
    Vector(size_t size) : size(size)
    {
        buffer = new T[size];
        for (size_t i = 0; i < size; ++i)
            buffer[i] = T();
    }

    ~Vector()
    {
        if (buffer != NULL)
            delete[]buffer;
    }

    T operator[](const size_t& idx) { /* ... */ }

private:
    size_t size;
    T* buffer;
};

then the debugger only shows me this (as one would expect looking at the structure of the class): enter image description here

Upvotes: 0

Views: 683

Answers (1)

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32742

You can create a Natvis visualization. The details are too long to go in to here, but see the Microsoft documentation for how to Create custom views of native objects in the Visual Studio debugger for how to create them.

Upvotes: 1

Related Questions