I am stuck...been at this for hours.
I overload the [] and << operators.
class IntArray;
int * arrayPtr;
so... IntArray obj1; // arrayPtr now points to a dynamic array.
when.. obj1[7] = 10; // no prolem, overload [] returns a reference of the dymanic array to store 10 in.
when.. obj1[8] = obj1[7]; HELP !
my assumption:
1. obj1[7] gets evaluate first, return a reference...then gets assigned to obj1[8] which is also a reference. How do I deref obj1[8] to get to the original value ? 10 in this case.
int& IntArray::operator[] (int i){
int temp = 0;
int indexTranslate = i - low();
if (indexTranslate > indexSize) {
cout<<"error ! Out of bound array assignment. \nattempt index: " << i <<" array max size is: "<< indexSize << endl;
}
// all arrays start with index 0. set arrayPtr to return first index
// next loop, add one to the index.
temp = ptrIncrement;
ptrIncrement++;
return(arrayPtr[temp]);
}
std::ostream& operator<<(std::ostream& os,IntArray& a){
// reset ptrIncrement to 0 for this [] overload.
a.ptrIncrement = 0;
for (int i = a.low(); i <= a.high(); i++) {
// if a[i] is a reference, dereference before cout.
// a ref should have an address.
os << a.getName()<<"[" << i << "] = " << a[i]<< " "<<endl;
}
a.ptrIncrement = 0; // reset for assignment call like a[x] = 33;
return(os);
}