本篇文章记录博主在学习链表时、编写复制构造函数中、为复制目标(新目标)分配内存时出现的问题。
先看一下赋值构造函数完成的方式,图一为原链表,要根据原链表为新链表一一动态分配内存,并赋值,赋值结束条件为原链表节点指针==NULL。
我出错的代码:
chainNode<T>* sourceNode = list.firstNode;
chainNode<T>* targetNode = new chainNode<T>(sourceNode->element);
firstNode = targetNode;
targetNode = targetNode->next;//①
sourceNode = sourceNode->next;
while (sourceNode != NULL)
{