在下面的代码中,我显式调用~destructor()
。但是该对象仍然可以访问。我怎样才能删除它(让它消失)?
class Queue {
public:
node* top = NULL;//points to the top of the queue
//methods:
void enqueue(int data);//adds a node to the queue
void dequeue();
//printing
void print();
//destructor
~Queue();
};
和析构函数:
Queue::~Queue() {
//The destructor deletes all items from HEAP
//Then sets the top to 0
while (top != NULL)
this->dequeue();//dequeue until there are NO more items
top = 0;
}
在 Source.cpp 中:
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.dequeue();
q.dequeue();
q.print();
q.~Queue();
q.print();//Here I need to have an ERROR!
q.enqueue(7);//Here I need to have an ERROR!
q.print();//Here I need to have an ERROR!
输出为:
4 3 7
我预计会出现错误:
identifier "q" is undefined
最佳答案
采用 //Here I need an ERROR!
从字面上看,您将如何做到这一点:
{
Queue q;
q.enqueue(1);
q.enqueue(2);
q.dequeue();
q.print();
}
q.print(); // THIS WILL PRODUCE AN ERROR
您似乎对堆栈分配对象的生命周期有误解。堆栈对象在超出范围时会自动销毁。在示例中,q
的范围以 }
结尾。
自己调用析构函数几乎总是错误的(我遇到了一种可以显式调用析构函数的情况)。为什么?考虑一下:
{
Queue q;
q.~Queue(); // DONT DO THIS !
}
您调用了析构函数,但当它超出范围时,它会再次被销毁,并且您将遇到令人讨厌的运行时错误。
您在代码中所做的事情:
Queue q;
q.~Queue();
q.print();
另请注意,删除对象时并不只调用析构函数。当删除堆栈分配的对象时,首先调用其析构函数,然后释放分配的内存。通常您不想干扰此过程,幸运的是您很少需要这样做。
How can I delete it(make it disappear)?
你无法让它“消失”。当对象被销毁时,内存中的位和字节不会被删除。那将是非常低效的。实际上,我认为 C 的 free
有一个更好且更容易混淆的名称。内存被释放以供以后使用,它不会被删除,以致无法读取之前的内容。
为了进一步阅读,我建议您引用这个稍微不同但相关的问题的详尽答案:Can a local variable's memory be accessed outside its scope?
另外,我建议您阅读 RAII它依赖于自动调用析构函数。
TL;DR:如果您想清除队列
,则写入:
q.clear();
q.print(); // prints an empty queue!
永远不要调用堆栈分配对象的析构函数!它将被自动调用。
关于c++ - 调用析构函数后访问对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55658281/