如何在链表类中实现赋值运算符 [英] How to implement an assignment operator in linked list class

查看:98
本文介绍了如何在链表类中实现赋值运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚如何在我的双向链表类中实现5的规则.我让他们的概念,它只是失去了如何编写它.我曾经尝试过使用析构函数和复制运算符,但在其他方面仍将继续.感谢您的任何帮助/指导.

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.

析构函数/副本:

~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

列出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;

    }

主要:

    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();

推荐答案

如果您有一个工作副本构造函数(确实不使用赋值运算符)和析构函数,则赋值运算符可以简单地是以下形式:

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;
 }

这基本上使用了复制/交换成语.根据传入的DList创建一个临时对象,该临时对象的内部与当前对象的内部交换.然后,临时人员将因旧的内部构件而死亡.

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.

这篇关于如何在链表类中实现赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆