From a9405d93c0e092eaac2884fe3fe2619fb40b8b89 Mon Sep 17 00:00:00 2001 From: tbarnes98 Date: Wed, 11 May 2022 14:58:42 -0500 Subject: [PATCH] week10 lecture 2 --- Lecture/Week10/main.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/Lecture/Week10/main.cpp b/Lecture/Week10/main.cpp index 35c490e..784ef61 100644 --- a/Lecture/Week10/main.cpp +++ b/Lecture/Week10/main.cpp @@ -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; } \ No newline at end of file