For School I have been given a class interface to define my own array class (not inherited).
I'm having trouble with overloading the assignment operator (=) and comparison operator (==) as well as the index operator.
This was given to me in the class interface:
Array& operator = (Array const& array1); // overload assignment operator
bool operator == (Array const& array1); // overload == operator
int const& operator [] (unsigned int index) const; // overload index operator
int& operator [] (unsigned int index); // overload index operator
What I can't seem to understand is how to assign it. If the array that you want to assign to your new variable is passed to the function = how do you return the value of that array to assign it the new variable. Or do I just allocate a new memory location and it automatically assigns it to what called the function?
With the == operator, how do I know what it's comparing to? How can I compare values, I don't know how to reference what called the function?
I pretty much have no idea how to overload the index operator. If the variable that is using the index operator points to the first int in the array, how do I increment it to return the value of the index that was called?
Not to mention, I'm not sure what exactly we're overloading it for, and the two overloading of the two index operators look the same. I don't know what to do for them.
My teacher told me the assignment operator is just like the copy constructor, which I wrote as this:
Array::Array(Array const& oldarray)
{
int *arraycopy;
arraycopy = new int[oldarray.length] // length is a member variable that has already been initiated
for(int i = 0; i < oldarray.length; i++) {
arraycopy[i] = oldarray[i];
}
}
Is it just the same thing for the assignment operator? I'm confused with all this and I will really appreciate any and all help! THanks!