linux - Move Assignment operator c++ -


i cannot tell wrong move assignment operator, here function. don't think grabbing data correctly, because when run test random negative number , "you're program has stopped working)

virtual linkedlist<t> &operator=(linkedlist<t> &&other) {     cout << " [x] move *assignment* operator called. " << endl;      // delete our own elements     listnode<t> *temp1 = _front;     while (temp1 != nullptr)     {         listnode<t> *n = temp1->getnext();         delete temp1;                    temp1 = n;     }     // grab other data ourselves     listnode<t> *temp2 = other._front;     while (temp2 != nullptr)     {         addelement(temp2->getvalue());         temp2 = temp2->getnext();     }     // reset pointers nullptr      other._front = nullptr;     other._end = nullptr;     other._size = 0;     other._last_accessed_index = 0;     other._last_accessed_node = nullptr;      return *this; } 

test code- teachers test code -

// use move *assignment* operator cout << " [x] test #5: move *assignment* constructor behavior" << endl; moved1 = linkedlist<int>{ 6, 7, 8, 9, 10 }; cout << "   [x] result:" << endl; cout << "   [x]  expected:\t6 7 8 9 10" << endl; cout << "   [x]  actual:\t\t"; (int = 0; < moved1.getsize(); i++) {     cout << moved1.getelementat(i) << " "; } cout << endl << endl; 

this first time working move , move assignment operator. :)

this not proper implementation of move assignment operator. looks more copy assignment operator (but not one, leaks memory).

a typical move assignment operator more instead:

#include <utility>  linkedlist<t>& operator=(linkedlist<t> &&other) {     cout << " [x] move *assignment* operator called. " << endl;      std::swap(_front, other._front);     std::swap(_end, other._end);     std::swap(_size, other._size);     std::swap(_last_accessed_index, other._last_accessed_index);     std::swap(_last_accessed_node, other._last_accessed_node);      return *this; } 

a move assignment operator should not free anything. move ownership of source's content target object, , vice versa. let source object free target object's previous content when source object destroyed after assignment operator exits, make sure class has proper destructor implementation:

~linkedlist() {     // delete our elements     listnode<t> *temp = _front;     while (temp != nullptr)     {         listnode<t> *n = temp->getnext();         delete temp;                    temp = n;     } } 

for measure, here copy constructor, move constructor, , copy assignment operators like:

linkedlist() :     _front(nullptr),     _end(nullptr),     _size(0),     _last_accessed_index(0),     _last_accessed_node(nullptr) {     cout << " [x] default *constructor* called. " << endl; }  linkedlist(const linkedlist<t> &src)     : linkedlist() {     cout << " [x] copy *constructor* called. " << endl;      listnode<t> *temp = src._front;     while (temp != nullptr)     {         addelement(temp->getvalue());         temp = temp->getnext();     } }  linkedlist(linkedlist<t> &&src)     : linkedlist() {     cout << " [x] move *constructor* called. " << endl;         src.swap(*this); }  linkedlist(initializer_list<t> src)     : linkedlist() {     cout << " [x] initialization *constructor* called. " << endl;      const t *temp = src.begin();     while (temp != src.end())     {         addelement(*temp);         ++temp;     } }  linkedlist<t>& operator=(const linkedlist<t> &other) {     cout << " [x] copy *assignment* operator called. " << endl;      if (&other != this)         linkedlist<t>(other).swap(*this);      return *this; }  linkedlist<t>& operator=(linkedlist<t> &&other) {     cout << " [x] move *assignment* operator called. " << endl;     other.swap(*this);             return *this; }  void swap(linkedlist<t> &other) {     std::swap(_front, other._front);     std::swap(_end, other._end);     std::swap(_size, other._size);     std::swap(_last_accessed_index, other._last_accessed_index);     std::swap(_last_accessed_node, other._last_accessed_node); } 

the copy , move assignment operators can merged single implementation, taking input object value , letting compiler decide whether use copy or move semantics when initializing object, based on context in operator called:

linkedlist<t>& operator=(linkedlist<t> other) {     cout << " [x] *assignment* operator called. " << endl;     swap(other);     return *this; } 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -