我有两个类:“节点”和“聚”。节点对象链接在一起形成一个链表。多对象包含一个指向第一个节点的指针。我试图为整个“poly”释放内存。我想删除poly - 然后在poly析构函数中调用一个函数(类似于“freePoly”),它将帮助我遍历整个链表的节点对象 - 删除所有节点。删除链接对象
下面是类的定义:
class Node
{
private:
double coeff;
int exponent;
Node *next;
public:
Node(double c, int e, Node *nodeobjectPtr)
{
coeff = c;
exponent = e;
next = nodeobjectPtr;
}
~Node()
{
printf("Node Destroyed");
//???
}
class poly
{
private:
Node *start;
public:
poly(Node *head) /*constructor function*/
{
start = head;
}
~poly() /*destructor*/
{
//???
}
void freePoly();
};
void poly::freePoly()
{
//???
}
我已经尝试了很多东西,但本质上我会被卡住在那里我只是删除第一个节点对象。然后我失去了指向其他节点的指针......并泄漏内存,因为我无法再访问它们以进行删除。
你不是在这里问这个? http://stackoverflow.com/questions/23200451/using-the-destructor-to-free-linked-objects – PaulMcKenzie