|
等 级:资深长老 |
经 验 值:27242 |
魅 力 值:4887 |
龙 币:14686 |
积 分:17746.1 |
注册日期:2002-11-25 |
|
|
|
请教C++编程高手一个问题,这个程序会不会跑飞?
#include <iostream>
using namespace std;
class Student
{
public:
Student (char *name);
~Student();
public:
char name[30];
Student *next;
static Student *point;
};
Student::Student (char *name)
{
strcpy(Student::name,name);
this->next=point;
point=this;
}
Student::~Student ()//析构过程就是节点的脱离过程
{
cout<<"析构:"<<name<<endl;
if(point==this)
{
point=this->next;
cin.get();
return;
}
for(Student *ps=point;ps;ps=ps->next)
{
if(ps->next==this)
{
cout<<ps->next<<"|"<<this->next<<endl;
ps->next=next;//=next也可以写成this->next;
cin.get();
return;
}
}
cin.get();
}
Student* Student::point=NULL;
void main()
{
Student *c = new Student("marry");
Student a("colin");
Student b("jamesji");
delete c;
Student *fp=Student::point;
while(fp!=NULL)
{
cout<<fp->name<<endl;
fp=fp->next;
}
cin.get();
}
我感觉,在main 里,执行delete c时的析构函数里,什么都没有做,但是*C在堆里申请的空间已经被释放掉了。这样,这个链表就不完整了。。。。
然后在执行while 循环时,fp有可能永远不是NULL...
哪位大拿帮忙解或?
|
|
|
|