链表C++中的赋值运算符

齐天大*_*天大圣 1 c++ constructor

我正在尝试在 C++ 中实现链表。

我像这样实现我的赋值运算符:

// assignment operator
template<class T>
LinkedList<T>& LinkedList<T>::operator = (const LinkedList& rhs) {

if (&rhs != this) {
    Node *tmp = head;

    while (tmp -> next) {
        head = head -> next;
        delete tmp;
        tmp = head;
    }

    tmp = rhs -> head;

    while (tmp) {
        append(tmp);
        tmp = tmp -> next;
    }
}

    return *this;
}
Run Code Online (Sandbox Code Playgroud)

在我的主函数中,我使用以下代码进行测试:

LinkedList<int> *lst1 = new LinkedList<int>(7);

LinkedList<int> *lst2 = new LinkedList<int>(101);

std::cout << lst1 -> head -> data << std::endl;
std::cout << lst2 -> head -> data << std::endl;

lst1 = lst2;

std::cout << lst1 -> head -> data << std::endl;

delete lst1;
delete lst2;            <--------------- error here
Run Code Online (Sandbox Code Playgroud)

正如我所料,控制台输出:

7 101 101

但是当程序尝试删除 lst2 时,我收到一条错误消息:

被释放的指针没有被分配

我使用调试器并找出程序何时进行分配:

lst1 = lst2;
Run Code Online (Sandbox Code Playgroud)

lst1 实际上指的是指向 lst2 的地址,而不是获取 lst2 的副本,所以当我删除 lst1 时,lst2 已经消失了。

那么任何人都可以告诉我我的赋值运算符有什么问题吗?

如果这是一个新手问题,我很抱歉,但我已经花了几个小时无法弄清楚。

我完成的代码如下所示:

template<class T>
class LinkedList {

private:
    class Node {
    public:
        T data;
        Node *next;

        // default constructor
        Node() = default;

        // constructor with data
        Node(const T& data) : data(data), next(NULL) {}

    };

public:
    Node *head;

    LinkedList(const LinkedList& copyLst);
    LinkedList& operator=(const LinkedList& byValList);
    LinkedList() : head(NULL){}
    LinkedList(Node *newNode) : head(newNode) {}
    LinkedList(T val) {
        head = new Node(val);
    }
    ~LinkedList();

    static LinkedList<int> sumLists(const LinkedList<int>& lst1, const LinkedList<int>& lst2) ;

    void insertAtFront(T val);
    void insertAtEnd(T val);
    void printList();
    void insert(T val);

    void append(const Node&);
};

// copy constructor
template<class T>
LinkedList<T>::LinkedList(const LinkedList<T>& copyLst) {

    const Node *cpCurrent = copyLst.head;
    Node *lsCurrent = NULL;

    if (cpCurrent != NULL) {
        head = new Node(cpCurrent -> data);
        lsCurrent = head;

        cpCurrent = cpCurrent -> next;

    }

    while (cpCurrent != NULL) {
        Node *newNode = new Node(cpCurrent -> data);
        lsCurrent -> next = newNode;

        lsCurrent = lsCurrent -> next;
        cpCurrent = cpCurrent -> next;
    }
}

// assignment operator
template<class T>
LinkedList<T>& LinkedList<T>::operator = (const LinkedList& rhs) {

    if (&rhs != this) {
        Node *tmp = head;

        while (tmp -> next) {
            head = head -> next;
            delete tmp;
            tmp = head;
        }

        tmp = rhs -> head;

        while (tmp) {
            append(tmp);
            tmp = tmp -> next;
        }
    }

    return *this;
}

// destructor
template<class T>
LinkedList<T>::~LinkedList() {
    Node *current = head;

    while (current != NULL) {
        head = head -> next;
        delete current;
        current = head;
    }
}

template<typename T>
void LinkedList<T>::append(const Node& node ){

    if (NULL == head) {
        Node *newNode = new Node(node -> data);
        head = newNode;
    } else {
        Node *current = head;

        while (current -> next) {
            current = current -> next;
        }


        Node *newNode = new Node(node -> data);
        current -> next = newNode;
    }
}
Run Code Online (Sandbox Code Playgroud)

Pau*_*zie 5

您当前的实现复制了复制构造函数中已经存在的代码,那么为什么不重用它呢?

如果你有一个工作拷贝构造函数和析构函数,那么使用拷贝/交换习语将是实现赋值运算符的最简单和最安全的方法。

#include <algorithm>
//...
template<class T>
LinkedList<T>& LinkedList<T>::operator = (const LinkedList<T>& rhs) 
{
    LinkedList<T> temp(rhs);
    std::swap(temp.head, head);
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

鉴于您的复制构造函数和析构函数正常工作,这可以保证正常工作。我们创建temp对象的副本rhs并将其内容替换为 的内容*this。当temp在返回时被销毁时,它会带走过去在*this.

如果你有一个 C++ 11 编译器,你可以利用传入参数的移动构造和传值。

#include <algorithm>
//...
template<class T>
LinkedList<T>& LinkedList<T>::operator = (LinkedList<T> rhs) 
{
    std::swap(rhs.head, head);
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

* 需要注意的是,使用复制和交换习语时需要交换类的所有成员,否则可能会使类成员的不变量失效*