0

I'm trying to work with dynamic arrays. When I try to overload the "=" operator it does not work. When debugging the file it doesn't execute the void function to overload the operator.

#include <iostream>
using namespace std;

class cppArray {
public:
    cppArray(int size);
    ~cppArray();
    int read(int index);
    void write(int content, int index);
    void operator=(cppArray& s);
    int search(int target);
    int size();
private:
    int* myArray;
    int arraySize;
};

cppArray::cppArray(int size) {
    myArray = new int[size];
    arraySize = size;
}

//delete the memory space assigned to myArray 
cppArray::~cppArray() {
    delete[] myArray;
    myArray = 0;
}

int cppArray::read(int index) {
    if (index < arraySize) {
        return myArray[index];
    }
    else {
        cout << "Out of range" << endl;
        exit(1);
    }
}

Here I'm trying to copy the content of the original array to an auxiliar one, and then redefine the size of the original array so I can add more content to the original array

void cppArray::write(int content, int index) {
    if (index < arraySize) {
        myArray[index] = content;
    }
    else {
        cppArray auxArray(arraySize);
        auxArray.myArray = myArray;
        delete[] myArray;
        arraySize = index + 1;
        myArray = new int[arraySize];
        myArray = auxArray.myArray;
        myArray[index] = content;
    }
}

I'm pretty sure this is wrong, but I can't figure out a way to overload it correctly

void cppArray::operator=(cppArray& s) {
    delete[] s.myArray;
    s.myArray = new int[arraySize];
    for (int i = 0; i < arraySize; i++)
    {
        myArray[i] = s.myArray[i];
    }
}

int cppArray::size() {
    return arraySize;
}

int main(int argc, char** argv) {
    cppArray dsArray(3);

    dsArray.write(1, 0);
    dsArray.write(2, 1);
    dsArray.write(3, 2);
    dsArray.write(4, 3);

    for (int i = 0; i < dsArray.size(); i++) {
        cout << dsArray.read(i) << "\t";
    }

    cout << endl;

    return 0;
}```
Cid
  • 17
  • 4
  • Please provide a [mcve], emphasize on "minimal". I didnt find where in the code you call the `operator=` – 463035818_is_not_a_number May 28 '20 at 20:43
  • Why don't you just use a [std::vector](https://en.cppreference.com/w/cpp/container/vector)? – Jesper Juhl May 28 '20 at 20:45
  • 3
    Also, the three legged stool of a proper [rule of three](https://en.cppreference.com/w/cpp/language/rule_of_three) appears to be missing a leg as well (namely the copy-ctor). And I think you should spend some time reading [What are the basic rules/idioms of operator overloading](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading), specifically the section on assignment operators, because yours has the wrong result type, and arguably the wrong argument type. – WhozCraig May 28 '20 at 20:47
  • 1
    One quick and easy way to implement `operator=` is to use the [Copy and Swap Idiom](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom). It also has the advantage of being as close to foolproof as you can get in programming. – user4581301 May 28 '20 at 20:47
  • 1
    `auxArray.myArray = myArray;` only copies a pointer. The you `delete` the memory it points to, invalidating `auxArray.myArray`. Then you allocate some new memory, only to immediately replace the pointer to it with the now invalid pointer that you saved. – molbdnilo May 28 '20 at 20:47
  • @JesperJuhl It is a learning excercise, in this case I would prefer to use std::vector – Cid May 28 '20 at 20:48
  • @WhozCraig Okay, I'll take a look on the material you recommended me, thank you – Cid May 28 '20 at 20:53

1 Answers1

3

Your implementation is almost correct, but you delete the wrong array. You should only modify *this object, and not s. Also, you should follow conventions, or people will be very surprised when using your class.

Here's corrected version:

//return *this - a very expected convention
cppArray& cppArray::operator=(const cppArray& s) {
// Make sure s is not modified ^^^^
    if (this == &s) {
        return *this; //protection against self writing, things would get bad if you did that
    }

    arraySize = s.arraySize; //copy size first
    delete[] myArray; //delete array from this object
    myArray = new int[arraySize]; //recreate array
    for (int i = 0; i < arraySize; i++)
    {
        myArray[i] = s.myArray[i]; //no changes here
    }

    return *this;
}
Yksisarvinen
  • 13,037
  • 1
  • 18
  • 42