0

I am having difficulty figuring out how to implement the rule of 5 in my doubly linked list class. I get the concept of them, it's just lost with how to code it. I have attempted the destructor and copy operator, but at a stand still going forward with the rest. Any help/guidance is appreciated, thanks.

destructor/copy:

~DList() {

    Node* current = front_;

    while (current != back_)
    {
            front_ = front_->next_;
            delete current;
            current = front_;

    }

}

    // copy ctor
    DList(const DList& rhs) {

            const Node* current = rhs.front_;
            Node* temp = nullptr;

            if (current != back_)
            {
                    front_ = new Node(current->data_);
                    temp = front_;
                    current = current->next_;
            }
            while(current != back_)
            {
                    Node* nn = new Node(current->data_);
                    temp->next_ = nn;
                    temp = temp->next_;
                    current = current->next_;
            }

    } 

DList& operator=(const DList& rhs){ //copy assignment // <<---not sure where to begin

List ctor:

    DList() {


        //Node* front_;
            front_ = new Node(); // creating front sentinel 
                                                     //Node* back_; 
            back_ = new Node(); // creating back sentinel
                                                    //make them point to eachother
            front_->next_ = back_;
            back_->prev_ = front_;
            listSz = 0;

    }

Main:

    DList<int> list;
    DList<int> list2;
    DList<int>::const_iterator it;

    cout << list.size() << endl;
    list.push_front(1);
    list.push_front(2);
    list.push_back(3);
    list.push_front(4);
    list.print();

    std::cout << endl;

    list2.push_front(11);
    list2.push_front(12);
    list2.push_back(13);
    list2.push_front(14);
    list2.print();

    list2 = list;
    list2.print();
bb13
  • 9
  • 6

1 Answers1

1

If you have a working copy constructor (that does not use the assignment operator) and destructor, the assignment operator can simply be this:

 #include <algorithm>
 //...
 DList& operator=(const DList& rhs)
 {
     DList temp(rhs);
     std::swap(temp.front_, front_);
     std::swap(temp.back_, back_);
     std::swap(temp.listSz, listSz);
     return *this;
 }

This basically uses the copy / swap idiom. A temporary object is created from the passed in DList, and the internals of the temporary object is swapped out with the internals of the current object. Then the temporary dies off with the old internals.

Community
  • 1
  • 1
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • i am looking for a deep copy method – bb13 Feb 25 '17 at 03:40
  • @bb13 -- That does a deep copy. I explained it in the answer. – PaulMcKenzie Feb 25 '17 at 03:40
  • that does a deep copy? – bb13 Feb 25 '17 at 03:41
  • ahhhh i see, I will look into it – bb13 Feb 25 '17 at 03:41
  • @bb13 -- Yes, it does a deep copy. The magic is the copy constructor usage on the first line. The other magic is the destructor usage when the function returns. Your copy constructor does the deep copy. – PaulMcKenzie Feb 25 '17 at 03:41
  • I was curious, is there a way to request the compiler to generate an assignment operator that does a swap? – Janarth K Feb 25 '17 at 03:46
  • @JanarthK Not that I know of. Right now, it's just rote boilerplate stuff. – PaulMcKenzie Feb 25 '17 at 03:48
  • @ PaulMcKenzie it gives me a runtime error when it hits list2 = list; – bb13 Feb 25 '17 at 03:49
  • @bb13 -- Then fix your copy constructor and/or destructor. My answer states right up front that your copy constructor and destructor must already be working correctly. The side benefit to writing the assignment operator this way is that it tests your copy ctor and destructor. Once you have both of those functions working, the assignment operator will automatically work correctly. – PaulMcKenzie Feb 25 '17 at 03:49
  • possibly a memory leak – bb13 Feb 25 '17 at 03:50
  • @PaulMcKenzie it does indeed seem to be the copy ctor that is faulty. program crashes with a line of DList list3 = list; – bb13 Feb 25 '17 at 04:03