这是我想澄清的事情。我有另一个显示链接的功能。调用该显示功能后,我的控制台上出现了垃圾值。 但当我评论“删除临时”声明它工作正常,我得到了预期的结果。请帮忙。谢谢。
void MyLinkedList::insertFirst(double data){
MyLink *temp = new MyLink(data);
temp->next = first;
first = temp;
delete temp;
}
答案 0 :(得分:1)
删除操作符不是用于删除指针本身,而是用于删除它指向的内存。
MyLink *temp = new MyLink(data); //allocate space for a MyLink dataType
first=tmp; //temp still points to the allocated space
delete temp; //deallocate the memory space pointed by temp which is the same memory space pointed by first.