#include <iostream>
#include<vector>
using namespace std;
typedef struct{
int x = 0;
int y = 0;
}Point;
class A{
public:
A(Point* s) {
p = s;
}
~A()
{
if (p) {
delete p;
p = NULL;
}
}
private:
Point* p;
};
int main()
{
Point* point = new Point();
A* a = new A(point);
if (point) {
delete point;
point = NULL;
}
if (a) {
delete a;
a = NULL;
}
}
point已被化为零,却没把类A的实例化里同一地址的p清零,怎么办??万分感谢!
你这里double deleting了。建议不要用裸指针,用智能指针,比如shared_ptr, weak_ptr, unique_ptr
因为A的构造函数是按值传递的,拷贝了一份指针,所以说A里面的p和main里面的point地址不同,当然不可以。你把30行到33行删去就行,多此一举。