C++ 链表赋值运算符 [英] C++ linked List assignment Operator

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

问题描述

尝试为单个链表类构建赋值运算符.我以为我正确构建了它,但仍然出现内存泄漏.

Trying to build an assignment Operator for a single linked list class. I thought I built it correctly, but am still getting a memory leak.

该类由一个 First 和 Last 变量组成.然后是一个 Node 结构.

The class consists of a a First and Last variable. And then a Node structure.

节点结构如下所示:

struct node
{
    TYPE value;
    node * next;
    node * last;
};

我的赋值运算符看起来像这样,它仍然存在内存泄漏

My Assignment Operator looks like this, it still has a memory leak

queue& queue::operator=(const queue &rhs){
            if(this == &rhs ){

            node *ptr = first;
             node *temp;
            while(ptr != NULL){
                 temp = ptr;
                 ptr = ptr->next;
                 delete temp; // release the memory pointed to by temp
            }
            delete ptr;


    } else{



        if (rhs.first != NULL ) {
                    first = new node(*rhs.first);
                    first->next = NULL;
                    last = first;
                    // first-> value = v.first->value; // copy constructor should have done that

                    node *curr = first;
                    node *otherCur = rhs.first;

                    node *ptr = first;
                     node *temp;
                    while(ptr != NULL){
                         temp = ptr;
                         ptr = ptr->next;
                         delete temp; // release the memory pointed to by temp
                    }


                    while(otherCur->next != NULL){
                        curr->next = new node(*otherCur->next);
                        curr->next->next = NULL;
                        last = curr->next;
                        // curr->next->value = otherCur->next->value;
                        curr = curr->next;
                        otherCur = otherCur->next;
                    }
                    // curr->next = NULL;
             }



    }
    return *this;
}

复制构造函数(工作):

Copy Constructor(working):

// copy constructor
queue::queue(const queue &v){
    if (v.first != NULL ) {
            first = new node(*v.first);
            first->next = NULL;
            last = first;
            // first-> value = v.first->value; // copy constructor should have done that

            node *curr = first;
            node *otherCur = v.first;
            while(otherCur->next != NULL){
                curr->next = new node(*otherCur->next);
                curr->next->next = NULL;
                last = curr->next;
                // curr->next->value = otherCur->next->value;
                curr = curr->next;
                otherCur = otherCur->next;
            }
            // curr->next = NULL;
        }


}

工作析构函数:

queue::~queue(){

    node *ptr = first;
     node *temp;
    while(ptr != NULL){
     temp = ptr;
     ptr = ptr->next;
     delete temp; // release the memory pointed to by temp
     }


}

.H 文件的成员变量:

Member variables of the .H file:

private:
    // fill in here
    node * first;
    node * last;

推荐答案

如果您有工作复制构造函数和析构函数,则可以使用 copy/swap.

Instead of all of that code, if you have a working copy constructor and destructor, the assignment operator can be implemented easily using copy / swap.

#include <algorithm>
//...
queue& queue::operator=(const queue& v)
{
   queue temp(v);
   std::swap(temp.first, first);
   std::swap(temp.last, last);
   return *this;
}

基本上所有所做的都是通过使用复制构造函数制作的临时副本.然后将 this 的成员与临时成员交换出去.然后最后,临时数据将被释放(析构函数),连同旧数据一起.

Basically all that's done is a temporary copy is made through using the copy constructor. Then the member(s) of this are swapped out with the temporary's members. Then at the end, the temporary will be deallocated (the destructor), taking along with it the old data.

我知道与您的尝试相比,代码很小,但它解决了其他人在评论中指出的所有问题,增加了异常安全等,最重要的是,它有效.

I know the code is tiny compared to your attempt, but it solves all the problems pointed out by others in the comments, adds exception safety, etc. and best of all, it works.

但请记住,您必须有一个有效的、无错误的复制构造函数和析构函数(此外,您的复制构造函数必须使用赋值运算符,不幸的是,许多不知情的程序员采取行动).此外,您必须交换所有成员变量,因此如果向 queue 类添加更多成员变量,则需要为每个新变量添加一个 swap.

But remember, you must have a working, non-buggy copy constructor and destructor (in addition, your copy constructor must not use the assignment operator, which unfortunately many unaware programmers resort in doing). In addition, you must swap all the member variables, so if you add more member variables to your queue class, you need to add a swap for each of those new variables.

请参阅有关复制/交换习语的信息.

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

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