I got a small problem. I have an array class and it returns an index as a reference so it can be accessed or modified.
Code:
// Allows accessing and modifying the array's contents.
Datatype& operator[] (const int p_index)
{
return m_array[p_index];
}
This works fine. But now I want to add bound checking.
I do this...
Code:
Datatype& operator[] (const int p_index)
{
if ((p_index >= 0) && (p_index <= m_size))
return m_array[p_index];
}
But the compiler complains about not all control paths return a value. So I do this...
Code:
Datatype& operator[] (const int p_index)
{
if ((p_index >= 0) && (p_index <= m_size))
return m_array[p_index];
else
return -1;
}
I get an error saying I cannot convert an 'int' to 'int &'. I want it to return something to indicate that it's an invalid cell like a boolean somewhat. That way we can know if it was successful or not when trying to modify/access the array element. Got any idea how this can be done?