【发布时间】:2014-05-27 02:57:32
【问题描述】:
我的链接列表中有一个Node 类和一个List 类。我需要在 Node 类中使用析构函数,还是应该只在 List 类中使用析构函数来删除所有节点?
到目前为止,这是我的 .h:
class Node
{
public:
int value;
Node* next;
Node();
Node(int);
};
class List
{
private:
Node* head;
public:
List();
List(List &a);
~List();
//additional functions (insert, delete, etc)
};
【问题讨论】:
-
您的代码会很有帮助...
-
你在 Node 中动态分配内存吗?如果是,您应该在其析构函数中取消分配它。否则 Node 的胆量将在超出范围时被清除。 List 中的 Node 对象也是如此,如果它们是动态分配的,那么你应该在 List 的析构函数中销毁它们。
标签: c++ linked-list destructor