Creating a copy constructor for a linked list

前端 未结 5 1668
Happy的楠姐
Happy的楠姐 2020-12-09 09:03

This is homework

I\'m working on implementing a linked list class for my C++ class, and the copy constructor has be very confusing for me.

T

5条回答
  •  轮回少年
    2020-12-09 09:49

    1. You shouldn't set this->head = v.head. Because the head is simply a pointer. What you need to do is to create a new head and copy the values individually from v.head into your new head. Otherwise you'd have two pointers pointing to the same thing.

    2. You then would have to create a temporary Elem pointer that starts with v.head and iterate through the list, copying its values to new Elem pointers into the new copy.

    3. See above.

提交回复
热议问题