C ++链表分配运算符 [英] C++ linked List assignment Operator

查看:97
本文介绍了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.

Node结构如下:

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.

有关复制/交换惯用法的信息,请参见此内容.

See this for information on the copy / swap idiom.

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

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