【发布时间】:2018-10-16 02:30:26
【问题描述】:
基本上,我已经创建了一个双向链表,现在需要证明析构函数已在 main.cpp(int main)中实现和测试。
在作业中建议我应该创建一个函数,并放置一个 cout 语句,该语句在每个节点被删除之前输出它的地址。我不知道该怎么做。
到目前为止,我已经创建了一个这样的函数:
void cList() {
DoublyLinkedList a
cout << "ex: 0 1 2 3" << endl;
for (unsigned i = 0; i < 4; i++) {
a.push_back(i);
}
cout << "Display in function: ";
a.display();
cout << endl;
//this shows the addresses of each node
cout << "delete addresses: " << endl;
for (it = del.begin(); it!=del.end(); ++it) {
cout << &it << endl;
}
}
我知道当列表超出范围时,将调用析构函数。我在 ~DoublyLinkedList 中有一个 cout 语句,上面写着“调用了析构函数”。当我这样做时,会在显示地址后输出“调用析构函数”语句。但是如何通过在删除之前显示地址来满足要求?谢谢。
样本输出:
delete addresses:
0x7ffeb484d300
0x7ffeb484d300
0x7ffeb484d300
0x7ffeb484d300
Destructor Called
【问题讨论】:
-
你有显示列表元素的功能吗?
标签: c++ destructor