我在删除节点内的对象指针时遇到问题。我想知道如果我只删除一个节点,对象指针也会释放吗?
struct customer{
int data;
customer(int data){
this->data= data;
}
};
class Node{
public:
customer * c;
Node * next;
Node(customer *&x){
this->c = x;
this->next = nullptr;
}
};
int main() {
customer * khach = new customer(5);
Node * node = new Node(khach);
delete node;
}
不,删除 Node 对象不会自动释放为客户对象分配的内存。您需要在
delete khach;
之前或之后显式 delete node;
以避免内存泄漏。