I'd like to have a class with two array subscript operator overloads: one used for reading and the other for writing.
The pourpose is to keep a counter of changes. I read (at http://faculty.cs.niu.edu/~mcmahon/CS241/c241man/node97.html) that I could do something like this:
template<typename T>
class Array
{
public:
Array()
{
data = new T[100];
}
T &operator[] (int index)
{
cout << "Is writing\n";
changes++;
return data[index];
}
T operator[] (int index) const
{
cout << "Is reading\n";
return data[index];
}
private:
T *data;
int changes;
};
But that doesn't work in my case. I'm using g++ 4.7 with -std=c++11 and actually only "Is writing" is printed on screen, even if I do:
Array<int> a;
a[0] = 3;
cout << a[0] << endl;
I also noticed that the latter is never called by inspecting the source with gcov. Is the method on that page completely wrong, or is something I'm misinterpreting?
Thanks in advance.