Search code examples
visual-c++visual-studio-debugging

Evaluating the subscript operator in the watch window


I have a simple array wrapper class, which goes like this:

class MyArray
{
    int * m_Data;
    int m_Size;

public:
    MyArray(int aSize) : m_Size(aSize), m_Data(new int[aSize])
    {
    }

    int & operator [](int aIndex)
    {
        return m_Data[aIndex];
    }

    const int & operator [](int aIndex) const
    {
        return m_Data[aIndex];
    }
};

MyArray a(10);

Whenever I try to evaluate a subscript operator in the debugger (quick watch, immediate window etc): e.g. a[0], I get a[0] no operator "[]" matches these operands error. I know I can dig through class fields to get to the content of the array. But it is so much easier to just copy a part of code line and evaluate it in the watch window. I tried removing const and non-const [] operators. I also tried using () operator, it didn't work either, but it gave a different error message. I tried this in VS2012 and VS2013 Preview: same thing. Is there any way to fix this?

If I replace the subscript operator with a member function:

int & Item(int aIndex)
{
    return m_Data[aIndex];
}

Then watch window is able to show me the result. But I would prefer to use subscript operator.


Solution

  • I found a solution, which is not very convenient, but seems to work. If I use the expanded form of the operator call, then it works in VC++2012:

    a.operator[](0)
    

    It's not clear to me why these two forms are different to VC++ debugger. So I posted a new question here