operator[] and assignment help !

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.

Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  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);
}
> when.. obj1[8] = obj1[7]; HELP !
> How do I deref obj1[8] to get to the original value ?
¿what?
you assign `foo' to `bar', ¿what's the issue?

Also, your operator[] looks funny. ¿are you using the parameter? ¿why is it modifying the state of the object?
Sorry...i am a 3 weeks old C++ programmer ! still trying to figure out my left from right.
Topic archived. No new replies allowed.