Linked List copy constructor

Dec 6, 2015 at 1:16am
I've always been bad with copy constructors with linked lists, and I need help with this one. Each node has m_data (the value contained in the node) and m_next (a pointer to the next node). m_head is a pointer to the head node, which is a dummy node. When printing the results of this copy constructor, it prints nothing.



What I've found through debugging is that it's copying each node into f just fine, but for some reason it's not changing the host object at all. All the copies just stay in f while the host linked list remains empty. I'm clearly doing something wrong in this copy constructor, any ideas what?
Last edited on Dec 7, 2015 at 8:05pm
Dec 6, 2015 at 4:16am
You never set m_head's m_next pointer to anything other than NULL. All of the nodes allocated in the loop are leaked.
Last edited on Dec 6, 2015 at 4:16am
Dec 6, 2015 at 5:05am
Thanks so much! Was able to fix it thanks to that. I keep expecting pointers to do things they can't.
Last edited on Dec 6, 2015 at 5:05am
Topic archived. No new replies allowed.