这是我拥有的一些代码的简化版本。由于pointerB
inclass A
设置为指针,beta
在指向分配内存的客户端代码中,我是否也必须释放pointerB
析构函数中指向的内存,class A
一旦它被删除?
class A{
public:
A(B* beta){
pointerB = beta;
}
~A(){
/*
would deleting pointerB be necessary
*/
}
B* pointerB;
};
class B{
public:
B();
};
//client code
B* beta = new B();
A* alpha = new A(beta);
//do stuff
delete beta;
delete alpha;
beta = NULL;
alpha = NULL;