Hello,

I'm building a simple array application, purely for my own personal edu purposes. The exercise is to overload the assignment operator.

I'm having problems with either Array::operator [], or Array::operator =. In the operator = function, the memory addresses are being set equal to the correct values:

0x002f2aa8 = 0;
0x002f2aac = 1;
0x002f2ab0 = 4;

The problem is when the [] operator is used after an assignment. The vales in the above mem addresses have been lost, and replaced with seemingly random values. Can anyone perhaps point out some things I might look into changing?

Here is the code:

Code:
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Array                     //models a normal C++ array
{
public:
	Array(int s)              //one-argument constructor
	{
		size = s;              //argument is size of Array
		ptr = new int[s];      //make space for Array
	}
	
	~Array()                  //destructor
		{ delete[] ptr; }
	
	int& operator [] (int j)  //overloaded subscript operator
	{ 
			return *(ptr+j); 
	}
	
	Array operator = ( Array& a )
	{
		if ( size != a.size)
			exit(1);

		for( int i=0; i<size; i++ )
			*(this->ptr+i) = a[i];

		return *this;
	}

private:
	int* ptr;                 //pointer to Array contents
	int size;                 //size of Array
};

////////////////////////////////////////////////////////////////
int main()
{
	const int ASIZE = 10;        //size of array
	Array arr(ASIZE);            //make an array

	for(int j=0; j<ASIZE; j++)   //fill it with squares
		arr[j] = j*j;

	Array arr2(ASIZE);
	
	arr2 = arr;

//	for(j=0; j<ASIZE; j++)       //display its contents
//		cout << arr[j] << ' ';

//	cout << "\n\n";

	for(j=0; j<ASIZE; j++)       //display its contents
		cout << arr2[j] << ' ';

	cout << endl;
	return 0;
}