#include<iostream>
using namespace std;
class TestClass{
public:
int *pPointer;
InIPoint(){
pPointer=new int;
}
~InIPoint(){
delete pPointer;
}
}a,b;
int main(){
*(b.pPointer)=89;
a=b;
cout<<"*(a.pPointer):"<<*(a.pPointer)<<endl;
cout<<"*(b.pPointer):"<<*(b.pPointer)<<endl;
return 0;
}
备注:new到局部新定义的指针了,函数返回这个地址就没了,new的内存也就那么放着没法用,也就是内存泄露。
#include<iostream>
using namespace std;
class TestClass{
public:
int *pPointer;
/*InIPoint(){
pPointer=new int;
}
~InIPoint(){
delete pPointer;
}*/
}a,b;
int main(){
b.pPointer=new int;
*(b.pPointer)=89;
cout<<"*(b.pPointer):"<<*(b.pPointer)<<endl;
a=b;
cout<<"*(a.pPointer):"<<*(a.pPointer)<<" "<<"*(b.pPointer):"<<*(b.pPointer)<<endl;
*(a.pPointer)=55;
cout<<"*(a.pPointer):"<<*(a.pPointer)<<" "<<"*(b.pPointer):"<<*(b.pPointer)<<endl;
delete b.pPointer;
cout<<"*(a.pPointer):"<<*(a.pPointer)<<" "<<"*(b.pPointer):"<<*(b.pPointer)<<endl;
return 0;
}
备注:不可以在类中new指针,否则会导致内存泄露.至于深拷贝、浅拷贝后续再研究研究.