假设我有一个结构,其中有两个指针,每个指针都指向一个具有已实现的析构函数的对象。还假设 head 指向一个具有非 NULL 值 *student 和 *next 的 Listnode 结构:
struct Listnode {
Student *student;
Listnode *next;
};
Listnode *head = new Listnode;
如果我在 Listnode 指针“head”上使用 delete
保留字,它将调用该结构 Student 类和 Listnode 类中的析构函数,其中“student”和“next”分别指向。换句话说,删除 *head 也会删除 *student 和 *next 提供的 head 是指向该 Listnode 的唯一指针
最佳答案
除非你的析构函数 ~Listnode
在指针上调用 delete
。但是,调用 delete
将调用非指针成员的析构函数。
关于c++ - 删除结构的指针是否也会删除结构内的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10081692/