赞
踩
1.在链表中,将某个指针delete ,指向该指针的那个指针的next 不会自动赋值为NULL 需要手动赋值。
2.删掉 某指针所指向的内存,该指针仍然可以使用。
下面是一个带头指针的单向链表
- void Stack::Pop(int &value){
- if(ndepth<=0){
- cout<<"the stack is empty"<<endl;
- return;
- }
- value=Sp->element;
- delete Sp;
- Sp=Header;//when the SP memonery is deleted,SP can point to other pointer.
- ndepth--;
-
- for(int i=0;i<ndepth;i++)
- Sp=Sp->next;
- Sp->next=NULL;// it is important here!
- }
3.关于拷贝构造函数:
我们往往会忽略为一个类显式的提供一个拷贝构造函数,c++编译器会为这个类产生一个缺省的拷贝构造函数,采用内存拷贝,一个字节一个字节的拷贝到新对象的内存中,这样新对象和老对象的内存映像是一模一样的。
显示提供 X (const X &obj);
例如:
- Stack(const Stack & s){
- SP=s.SP;
- nDepth=s.nDepth;
- Cells=new int[nDepth];//we should apply for a new memory for the Cells.
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。