Using Overloaded [] with Overloaded =

Hey, I am trying to write a statement in my program like this:

 
c[index] = value



where c is an object I have created with an int array member variable and index and value just int variables in main.cpp

Here are my overloaded definitions:


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
27
28
29

DynamicIntArray & DynamicIntArray::operator=(const DynamicIntArray & right)
{
	int * newArray = new int[right.capacity];
	for (int i = 0; i < right.length; i++)
		newArray[i] = right.array[i];
	delete[] array;
	array = newArray;
	capacity = right.capacity;
	length = right.length;

	assert(this != &right);
	assert(array != right.array);
	
	return *this;
}


int DynamicIntArray::operator[](int index) const
{
	if (index < 0 || index > length - 1)
	{
		cout << "Error. Index out of range." << endl;
		return array[0];
	}
	return array[index];

}


Of course, the command does not seem to work because the overloaded assignment operator is supposed to work with objects of class DynamicIntArray. Can anyone give me a suggeston as to how I could write a working statement like this?
Make another, non-const variant of operator[] which will return value by reference.
Topic archived. No new replies allowed.