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!
First and foremost it’s important you understand that the operators you mentioned work on
this. Each give you different variables depending on the operator you choose to overload:Assignment operator:
This operator means “assign
array1tothis“, it is also known as “copy operator”.Normally it would be implemented by copying
array1intothisdepending on the data type. This can be achieved usingmemcpyfor basic arrays.Comparison operator:
Again, this means “compare
thistoarray1“. It should be pretty straight forward – use whatever logic the data type is using to determine if the data inthisis equal to the data inarray1. This can be achieved usingmemcmpfor a basic array type.Index operators
Those are basically the same operator, however one of them is “read only” version where you cannot write to the array.
This operator may be confusing, because it does not mean “put item at index”, but it means “give me a reference to index where I can put my item”. This means that the assignment is done externally, e.g, the statement:
Is actually two statements (note: this assumes your Array class is an Array of
ints):The implementation of this operator varies upon the implementation of your data type.