week10 lecture 2

This commit is contained in:
2022-05-11 14:58:42 -05:00
parent 54951669ba
commit a9405d93c0

View File

@@ -6,6 +6,7 @@ class Row{
int size = 0;
double * data;
// constructor
Row(int size){
cout << "constructor" << endl;
this->size = size;
@@ -22,6 +23,14 @@ class Row{
}
}
// move copy constructor
Row(Row&& from){
cout << "move copy constructor" << endl;
this->size = from.size;
this->data = from.data;
from.data = nullptr;
}
// assignment operator (non-move)
Row operator=(const Row& rhs){
if(this != &rhs){
@@ -35,15 +44,35 @@ class Row{
return *this;
}
Row& operator=(Row&& from){
cout << "assignment operator" << endl;
if(this != &from){
delete[] data;
//this-size = from.size; // gives error
this->data = from.data;
from.data = nullptr;
}
return *this;
}
~Row(){
delete[] data;
}
};
void swap(Row& R1, Row& R2){
Row temp = move(R1);
R1 = std::move(R2);
R2 = std::move(temp);
}
int main(){
Row R1(2);
Row R2(R1);
Row R2(3);
swap(R1,R2);
return 0;
}