single_list::single_list(const single_list &sl) { cout << "copy_constructor stat!" << endl; if (sl.head == NULL) head = NULL; Node* pt = head = new Node; Node* pl = sl.head; pt->data = pl->data; while (pl->next) { pt = pt->next = new Node; pl = pl->next; pt->data = pl->data; } size = sl.size; cout << "copy_constructor End!" << endl; } |
struct Node { void* data; Node* next; }; |
原帖由 成古样个 于 2009-8-6 10:50 发表
single_list::single_list(const single_list &sl)
{
cout << "copy_constructor stat!" << endl;
if (sl.head == NULL)
head = NULL;
原帖由 huangwei0413 于 2009-8-6 11:23 发表
不晓得其他成员函数咋写的,但是至少拷贝构造函数有个问题,最后一个Node的next没有置空:
在
while (pl->next)
{
pt = pt->next = new Node;
pl = pl->next;
pt->data = pl ...
if (sl.head == NULL) { head = NULL; size = 0; return; } |
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |