1

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!

4 Answers 4

4

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:

Array& operator = (Array const& array1);

This operator means "assign array1 to this", it is also known as "copy operator". Normally it would be implemented by copying array1 into this depending on the data type. This can be achieved using memcpy for basic arrays.

Comparison operator:

bool operator == (Array const& array1);

Again, this means "compare this to array1". It should be pretty straight forward - use whatever logic the data type is using to determine if the data in this is equal to the data in array1. This can be achieved using memcmp for a basic array type.

Index operators

int& operator [] (unsigned int index);
int const& operator [] (unsigned int index) const;

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:

myarray[idx] = item;

Is actually two statements (note: this assumes your Array class is an Array of ints):

// Reference to the index location
int& writeLocation = myarray[idx];

// Write item to the location
writeLocation = item;

The implementation of this operator varies upon the implementation of your data type.

Sign up to request clarification or add additional context in comments.

5 Comments

so would this be right for the read only index operator: int const& Array::operator [] (unsigned int index) const { return this->data[index]; } Where data is the member variable holding the array variables?
That looks perfectly correct! Please note that this->member is redundant in C++ in most situations, and { return data[index]; } would work too.
Thank you so much I really appreciate it. I did the exact same thing for both index operators, should they be the same or do I need anything additional for the non read only index operator?
The fact that you have made the reference const (return value of type int const&) will make the compiler take care of the 'read only' part. Having both implementation have the same code is correct - it can even be called "const correct". Don't forget to select the correct answer when you're done!
All of the answers have helped me, but you helped me the most. I really appreciate it thanks!
2

With the assignment operator your array could already be allocated (ie a size > 0). So you would need to free any existing memory / or some other scheme to assure that both of your arrays are the same size. The assignment operator should return *this to return a reference to the current array for things like assignment chaining.

For bool operator == (Array const& array1); you would want to compare all the values in array1 to this (the called object). You could optimize this a bit by first comparing the sizes of the arrays to make sure they are the same.

2 Comments

Ok thank you for helping. But I wrote this and I got an error: bool Array::operator == (array const& array1) { if (this.length() != array1.length) } and already it gave me an error on the 'this' keyword "Error: expression must have class type"
this is a pointer you need to dereference it ie. this->length() and array1.length()
0

when you write :

a = b; 

Your c++ compiler calls :

a.operator=(b). 

Therefore in your operator=() implementation, you need to assign object b value to object a (and then return a). Remember that in a member function the a can be accessed through the pointer this.

Comments

0

In general, assignment operator looks like

Array& Array::operator = (Array const& rhs)
{
    if (*this != rhs) // prevent self-copying
    {
        delete[] this->array;
        this->array = new int[rhs.length];
        //copy array;
        // copy all other members - if they are pointers, don't forget to free memory first;
    }
    return *this;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.