0

I'm having quite the issue writing an overloaded assignment operator for the class below:

namespace w3{

    class DArray2d{

        int rows;
        int columns;
        double **array;

    public:

        DArray2d();
        DArray2d(int, int);
        DArray2d(const DArray2d &obj);
        DArray2d& operator=(DArray2d& rhs);
        DArray2d(const DArray2d &&obj);
        DArray2d& operator=(DArray2d&& rhs);
        double check() const;
        ~DArray2d();
    };

}

Here is what I attempted, but I'm getting a segmentation fault:11 when I try to copy each element in the for loop:

w3::DArray2d& w3::DArray2d::operator=(w3::DArray2d& rhs){

    // std::cout << "assignment operator" << std::endl;

    if ( this != &rhs )
    {   


        this->rows = rhs.rows;
        this->columns = rhs.columns;

        std::cout << this->rows << std::endl;
        std::cout << this->columns << std::endl;

        for(int a = 0; a < rhs.rows; a++){
            for(int b = 0; b < rhs.columns; b++){
                array[a][b] = rhs.array[a][b];
            }
        }
    }

    return *this;

}

I have a copy constructor, could I make use of that?

w3::DArray2d::DArray2d( const DArray2d &obj):rows(obj.rows), columns (obj.columns) {

    // std::cout << "copy constructor" << std::endl;

    if(this != &obj){

        array = new double*[obj.rows];
        for(int i = 0; i < obj.columns; i++){
            array[i] = new double[columns];
        }

        for(int a = 0; a < obj.rows; a++){
            for(int b = 0; b < obj.columns; b++){
                array[a][b] = obj.array[a][b];
            }
        }

    }

}

Also, is this the proper way to delete a 2d array?

for (int a = 0; a < rows ; ++a){
    delete [] array[a];
}
delete [] array;
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Mmm Donuts
  • 9,551
  • 6
  • 27
  • 49
  • 1
    Use `std::vector` as storage and let compiler generate assignment. Easy-peasy. ;-) – Cheers and hth. - Alf Sep 22 '14 at 23:57
  • 1
    If dimensions of `this` array are different from those of `rhs`, you copy dimensions over, but you don't re-allocate memory. If you assign a 100x100 array to a 1x1 array, `this->array` won't magically become bigger - your program would have a buffer overrun. – Igor Tandetnik Sep 23 '14 at 00:00
  • I can't... I wish I could. This is part of an assignment that I guess is aimed towards teaching how the inner workings of C++ function. So I'm stuck :( – Mmm Donuts Sep 23 '14 at 00:00
  • 1
    Yes you can make use of the copy constructor: [What is the copy-and-swap idiom?](http://stackoverflow.com/q/3279543/3959454) – Anton Savin Sep 23 '14 at 00:02
  • Ok. So I have the line a = b in main. 'b' is 5000x5000. 'a' is empty. So that's my problem. How do I use the copy constructor to bring 'a' up to a 5000x5000 array? I'm guessing that's what I need to do here. – Mmm Donuts Sep 23 '14 at 00:04
  • 1
    Your assignment operator needs to delete the old array and allocate a new one of the right size. Copy-and-swap is a good method. – Neil Kirk Sep 23 '14 at 00:07
  • Ok. So I delete the array, invoke the copy constructor to create a temporary object. Swap that object with 'this', and then allocate the array to 'this'? – Mmm Donuts Sep 23 '14 at 00:11
  • Pass the parameter to the assignment operator by value, which performs the copy, and then `swap(*this, other)` or however you implemented the swap function. This will put the pointer to the old array in the `other` parameter. When the function ends, `other` goes out of scope and deletes the old array automatically. – Neil Kirk Sep 23 '14 at 00:16
  • I've never used the copy and swap method before. But I guess i'll see what I can do. – Mmm Donuts Sep 23 '14 at 00:22
  • does the assignment require you to have `double **array;`? can you instead have a `unique_ptr` (or write a `non_std::vector`) and calculate the indexes manually? – Caleth Jul 04 '18 at 10:28

0 Answers0