Suppose the following is the [] operator implementation for vector:
template<class T>
T& Vector<T>::operator[](unsigned int index)
{
if(index >= my_capacity) {
if(1 /*something to check that [] operator was used in RHS (means read)*/) cout << "Out of bounds read" << endl;
else cout << "Out of bounds write" << endl; //means write operation
}
return arr[index];
}
And now inside main():
Vector<int> v(5, 10); //initializes 5 values, each = 10
Now, let's say I have 2 statements
int ans = v[10]; //First
v[10] = 1; //Second
I basically want to get the output as:
Out of bounds read
Out of bounds write
How should I decide the condition inside the 'if' for the same (that whether the call to [] operator is a read or a write one)? Can anyone please help me in this? Thanks a lot!