简体   繁体   中英

Overloading the + operator with a single-linked list

For my homework assignment I have to create an int Linked List. 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; It works. It seems that the destructor is called right before it returns, so I get nothing for List3 if I do

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+=. However, you should declare both LHS and RHS const when overloading operator +.

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:

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.

Assuming the default constructor is an empty list and your class doesn't use pointers, try declaring const correctly. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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