简体   繁体   English

用单链表重载 + 运算符

[英]Overloading the + operator with a single-linked list

For my homework assignment I have to create an int Linked List.对于我的家庭作业,我必须创建一个 int 链表。 I have the copy constructor and assignment operator overloaded, but can seem to get the + operator overloaded.我重载了复制构造函数和赋值运算符,但似乎可以重载 + 运算符。 I have a defined destructor that clears the list.我有一个明确的析构函数来清除列表。

List List::operator+(const List &add)
{
     List result;
     result += *this;
     result += add;
     return result;
}

The += is working. += 正在工作。 Also, when I do something like the following: List list3 = list1 + list2;另外,当我执行以下操作时: List list3 = list1 + list2; It works.有用。 It seems that the destructor is called right before it returns, so I get nothing for List3 if I do似乎析构函数在它返回之前就被调用了,所以如果我这样做,我对 List3 一无所获

List list3;    
list3 = list1 + list2;

Here is the copy constructor, assignment overload, and += overload这是复制构造函数、赋值重载和 += 重载

List& List::operator=(const List &assign)
{
    Node *traverse = assign.head;
    int x;
    int *passX = &x;
    while (traverse != nullptr)
    {
        x = traverse->getItem();
        this->Insert(passX);
        traverse = traverse->getNext();
    }
    return *this;
}

List342& List342::operator+=(const List342 &add)
{
    Node *traverse = add.head;
    int x;
    int *passX = &x;
    while (traverse != nullptr)
    {
        x = traverse->getItem();
        this->Insert(passX);
        traverse = traverse->getNext();
    }
    return *this;
}

List342::List342(const List342 &copy)
{
    *this = copy;
}

struct Node
    {
        int item;
        Node *next = nullptr;
        int getItem() const;
        Node* getNext() const;
        void setItem(const int &val);
        void setNext(Node* nodePtr);
    };
    Node *head;
    int itemCount;

The last portion is the struct for the node, and the two variables that the any object of this class will have.最后一部分是节点的结构,以及这个类的任何对象将拥有的两个变量。

Thanks谢谢

So I managed to figure this out所以我设法弄清楚了这一点

List& List::operator+(const List &add)
{
    List *result = new List;
    *result = *this;
    *result += add;
     return result;
}

My destructor was clearing the list before its return... Allocating space in the heap prevents the destructor from doing this.我的析构函数在返回之前清除了列表......在堆中分配空间可以防止析构函数这样做。

Thanks for the help everyone!感谢大家的帮助!

Good job implementing operator+ as a function of operator+=.很好地将 operator+ 实现为 operator+= 的函数。 However, you should declare both LHS and RHS const when overloading operator +.但是,在重载运算符 + 时,您应该同时声明 LHS 和 RHS const。

const List List::operator+(const List &add) const
{
     List result;
     result += *this;
     result += add;
     return result;
}

The first const means that the function returns a const List, so you can't:第一个 const 意味着该函数返回一个 const 列表,因此您不能:

List A, B, C;
A + B = C;

The second one means that the function doesn't modify its' member variables, so you can trust that A+B doesn't modify A.第二个意味着该函数不会修改其成员变量,因此您可以相信 A+B 不会修改 A。

Assuming the default constructor is an empty list and your class doesn't use pointers, try declaring const correctly.假设默认构造函数是一个空列表并且您的类不使用指针,请尝试正确声明 const。 Const variables scope differently than non-const.常量变量的作用域与非常量不同。 If your class uses pointers, make sure you're actually copying the relevant memory, not just the memory addresses.如果您的类使用指针,请确保您实际上是在复制相关内存,而不仅仅是内存地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM