Dymanic array Matrix

For a project we have to make a class and use a dynamic array to create several matrices. We cannot use a Vector. We have to write our copy constructor, destructor, assignment operator, move constructor, move assignment operator.
I have several questions.
1)Can someone help me come up with the move functions. I am confused on how they work
2) would this be ok for my copy and assignment functions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  Matrix(const Matrix& other): myArray(0), _size(0)
{
   if(this != &other) {
      Matrix my_tmp_a(other._size);
      std::copy(&other[0], &other[other._size], &my_tmp_a[0]);
      swap(my_tmp_a);
   }
}

const Matrix& operator=(const Matrix& other)
{
   if(this == &other) return *this;
   Matrix my_tmp_a(other._size);
   std::copy(&other[0], &other[other._size], &my_tmp_a[0]); 
   swap(my_tmp_a);       
   return *this;
}

void swap(const Matrix& other) {
   int* my_tmp_array = this.myArray;
   this.myArray = other.myArray;
   other.myArray = my_tmp_array;
   int my_tmp_size = this._size;
   this._size = other._size;
   other._size = my_tmp_size;
}

for the move look at iso c++11 , but you should have a && in your param .

For the rest look at allocators and pools.
Answering there instead of your newer post as this one actually has code and more full info.

1) Your copy constructor is a mess. Swap idiom generally is not applied to copy constructors. Additionally you do not need to check for self...copying? Look into delegating constructors too, as you seem to might use them here.

2) When you do swap-based assigment operator you do not need self-assigment check and you should fully delegate all work to copy constructor. Canonical copy/move-assigment operator:
1
2
3
4
5
6
const A& A::operator=(A other)
{
    using std::swap; //Allowing user-defined swaps to be ADL-found
    swap(*this, other);
    return *this;
}
Note taking argument by value. It allow it to work in both copy and move contexts.

You will either need to implement move constructor and use std::swap (will not work with old C++ versions) or roll your own swap for your class and do memberwise swapping.
Last edited on
So in the assignment function what is temp? Also will this work even though it's a matrix?
Sorry, didn't rename everything properly. Fixed.

Also will this work even though it's a matrix?
It will work for everyhing with properly implemented move constructors or own swap function.
Last edited on
Thanks,
I think I need to fix my destructor. I tried a couple of ways to implement it.
None of them have worked
1
2
3
4
5
6
7
8
9
10
template <typename Obj>
Matrix<obj>::~Matrix(){//destructor
 for (int n=0; n< num_row; n++){
            for (int m=0; m< num_col; m++){
                delete [] array_[n][m[;
            }
        }
        delete [] array_;
        array_=nullptr;
}

1
2
3
4
5
6
7
8
9
template <typename Obj>
Matrix<obj>::~Matrix(){
     for (int n=0; n< num_row; n++){
            for (int m=0; m< num_col; m++){
                delete [] *(array_+1);
            }
        }
        delete [] array_;
}

and this way
1
2
3
4
5
6
7
8
9
10
template <typename Obj>
Matrix<obj>::~Matrix(){
        	for (int i = 0; i < num_row; i++){
                delete [] array_[i];
                array_[i]=nullptr;
        	}
	delete [] array_;
	array_=nullptr;

  }
What is inside your Array?pointers?
I actually fixed it. the error was with my assignment operators.
I was wondering if you can take a look at them.

If X is a 2X2 matrix 2 2
2 2
and Y is a 3X3 matrix 3 3 3
3 3 3
3 3 3
X = Y;
cout << a << endl;
This should look like:
3 3 3
3 3 3
3 3 3

Instead I am getting:
3 3 3
3 3 3
I figure it has to do with allocating room because X can be bigger than Y and vice versa.
1
2
3
4
5
6
7
8
9
10
11
template <typename obj>
Matrix<Obj>& Matrix<Obj>::operator=(Matrix<Obj> &rhs){
  if(this != &rhs){
        for (int n=0; n< rows; n++){
            for (int m=0; m< cols; m++){
            my_array[n][m]= rhs.my_rray[n][m];
            }
        }
    }
	return *this;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <typename obj>//move assignment
Matrix<Obj>& Matrix<Obj>::operator=(Matrix<Obj> &&rhs){
  if(this != &rhs){
        for (int n=0; n< rows; n++){
            for (int m=0; m< cols; m++){
            my_array[n][m]= rhs.my_array[n][m];
            }
        }
    }
     rhs.my_array_=nullptr;
    rhs.row=0;
    rhs.cols=0;
	return *this;
}
Last edited on
Topic archived. No new replies allowed.