In an old C++ book I have, the following example is given for overloading the [] operator:
int &IntArray::operator[] (int index)
{
static int dummy=0;
if((index=0) && (index < length))
return array[index];
else
{
cout << "Error: index out of range.\n";
return dummy;
}
}
where length and array[] are member variables of the class IntArray.
My question is as follows. Suppose you have this code:
int x;
{
IntArray somearray;
x=somearray[0];
}
Dosomethingwith(x);
Since somearray[0] returns a reference to data in somearray and somearray goes out of scope before x does, wont bad things happen when Dosomethingwith() tries to use x?