动态内存的释放
假如说有两个类..class A
{
public:
A();
~A();
void Cleanup();
private:
int* p;
....
};
class B
{
public:
B();
~B();
void Cleanup();
private:
A* a;
....
}
A::A()
{
p = new int(2);
}
A::~A()
{
Cleanup();
}
void A::Cleanup()
{
delete p;
p = NULL;
}
如果我在B的构造函数中这样调用
B::B()
{
a = new A();
}
void B::Cleanup()
{
a->Cleanup();
delete a; //请问以下两句是必须的吗? 有必要再去delete a吗?
a = NULL;
}
void B::~B()
{
Cleanup();
}