Search code examples
c++copyoverloadingmove

Do I need to delete current member data when overriding copy/move assignment overload?


I am studying for a final tomorrow in a c++ class, and one thing that I was thinking about while studying was what happens to assigned-to object data after a copy/move assignment overload is called.

Say we have this class:

class linked_list {

public:
    //You need to implement this part.
    int num_nodes;
    node * head;

linked_list() { num_nodes = 0; head = nullptr; }

~linked_list(); //dectructor
linked_list(const linked_list &other); //copy constructor
void operator=(linked_list &other); //copy assignment overload
linked_list(linked_list &&other); //move constructor
void operator=(linked_list &&other); //move assignmend overload

};

I realized that throughout the whole semester I've been just copying in/taking ownership of the other data, without ever worrying about the previous data in the assigned-to object.

void linked_list::operator=(linked_list &other) { //copy assignment overload
    num_nodes = 0;
    head = nullptr;
    node * n = other.head;
    while (n != nullptr) {
        push_back(n->value);
        n = n->next;
    }
}

void linked_list::operator=(linked_list &&other) { //move assignment overload
    num_nodes = other.num_nodes;
    head = other.head;
    other.head = nullptr;
}

With the two examples that I have here, wouldn't the data of all the nodes in the current (this) linked_list still be floating around in memory? Before doing my copying/moving, would I have to iterate through the entire current linked list and delete each node?

For some reason I get the vibe from both other students and the course material that the class destructor is implicitly called before entering the two overloaded functions, because NOWHERE do I ever see any mention about having to free up current data first.

Any clarification would be extremely helpful.

Thank you,


Solution

  • Your concerns are justified. Yes, every time you implement assignment operator (copy or move or any other kind), you are responsible for proper deallocation of "old" data potentially present in (and owned by) the object before copying/moving the new data in.

    One can roughly describe the functionality of assignment operator as a combination of destructor call followed by copy/move constructor call (don't take it literally: this is not a good way to implement the assignment operator). The destruction of "old" data is not done automatically for you, i.e. there's no automatic implicit destructor call before invocation of your assignment operators, contrary to the vibe you say you got from the course material. It is your responsibility to manually implement the destruction of "old" data.

    Take a look Copy-And-Swap idiom as one possible idiomatic way to implement assignment operators.

    Of course, all this applies only if your class's members don't handle the proper assignment semantics themselves (in accordance with the Rule of Zero). In your example you are using owning raw pointers, meaning that it is your responsibility to perform the required resource management steps manually.