当调用析构函数时,是否可以将指向类实例的指针设置为nullptr ?

时间:2022-09-03 07:17:08

I am attempting to create a linked list for a class project. My node class has a pointer which points to the linked node and another pointer which points to a specialized book class.

我正在尝试为一个类项目创建一个链表。我的node类有一个指向链接节点的指针,还有一个指向专用book类的指针。

class Node{
private:
    Book *data;
    Node *linkedLink;
public:
    Node(Book *inputedData);
    Book* getBook();
    Node* getLinked();
    void changeLinked(Node *newLink);
    Node& operator=( const Node& rhs );

    ~Node(); //deconstructor
};

My professor has provided a driver that we cannot change. In this driver, he has the following code:

我的教授给了我们一个无法改变的司机。在这个驱动中,他有以下代码:

// (bookList is an instance of my LinkedList class)
// (and getFirst() returns my LinkedList's head node)

while (bookList.getFirst() != nullptr)
{
    Node * t = partsList.pop_front();
    delete t;
}

The following is my node classes destructor:

下面是我的节点类析构函数:

Node::~Node(){
    delete data; //this deletes the book class that data points to.
    delete linkedLink;
}

The problem is, I need the variable (t) that points to the instance of a Node class to be set to nullptr after it's done releasing *data and *linkedLink. But I can't change my professors driver, which means that I would have to do this in my Node classes destructor. I'm stumped on how I would do this... Wouldn't I have to set the Node class to nullptr to break out of the while loop from my professors driver? Or maybe I'm completely off track and overthinking this. Any help would be greatly appreciated.

问题是,我需要将指向节点类实例的变量(t)设置为nullptr,在它释放*data和*linkedLink之后。但是我不能改变我的教授驱动,这意味着我必须在我的节点类析构函数中这样做。我不知道该怎么做……我不需要将节点类设置为nullptr来从我的教授驱动程序中跳出while循环吗?也许我完全偏离了轨道,想得太多了。如有任何帮助,我们将不胜感激。

=======[ Edit ]=======

[编辑]= = = = = = = = = = = = = =

My Node class can only point to one other Node class, no more. It must be a "singly linked" list, not "double linked".

我的节点类只能指向另一个节点类,不能指向其他节点类。它必须是一个“单独链接”的列表,而不是“双重链接”。

=======[ Edit 2.0 ]========

= = = = = = = 2.0[编辑]= = = = = = = =

for those asking for more code, the following is my LinkedList class.

对于那些需要更多代码的人,下面是我的LinkedList类。

class List{
private:
    Node* head;
    Node* tail;
    int count;
public:
    List();
    List(Node firstLink);

    ~List(); //deconstructor

    Node* getFirst() const;
    Node* getLast() const;

    void push_back(Node *data);
    void push_front(Node *data);

    Node* pop_front();
    Node* pop_back();

    int getLength() const;
    List& operator=(const List& that);
};

1 个解决方案

#1


5  

There is no facility in the C++ language to automatically set some pointer somewhere to null, when an object gets destroyed. If some pointer needs to be set to null, somewhere, it is the destructor's responsibility to make it happen. Presumably, in some fashion you need to implement some kind of facility that keeps track of pointers to the class instance, and then in the destructor those pointers will be set to null.

在c++语言中,当一个对象被销毁时,不存在自动将某个指针设置为null的功能。如果某个指针需要设置为null,那么发生这种情况是析构函数的责任。假设,在某种程度上,您需要实现某种工具,以跟踪指向类实例的指针,然后在析构函数中,这些指针将被设置为null。

Having said that, I'm confident that you are misinterpreting your assignment. I see no need whatsoever for any pointer to be set to null, when the class instance gets destroyed.

话虽如此,我相信你误解了你的作业。当类实例被销毁时,我认为任何指针都不需要设置为null。

In your professor's immutable driver, the call to the pop_front() method is going to remove the node from its linked list completely, and return the pointer to the removed node. Your implementation of pop_front() is going to completely remove the node from the list, updating all list pointers accordingly, to point to the remaining elements in the list.

在教授的不可变驱动程序中,对pop_front()方法的调用将完全从其链表中删除节点,并返回指向已删除节点的指针。pop_front()的实现将完全从列表中删除节点,相应地更新所有列表指针,以指向列表中的其余元素。

At this point, no other pointers to the class instance should exist, except for the pointer being returned from pop_front(), and this pointer then immediately gets deleted. There won't be any pointers to your class instance that need to be set to null.

此时,除了pop_front()返回的指针之外,不应该存在指向类实例的其他指针,然后立即删除该指针。不会有任何指向类实例的指针需要设置为null。

Your professor's code is the classical implementation of std::list, which manages the exact same task without requiring the destructors of any class instance in std::list to worry about setting any pointer to null.

您的教授的代码是std::list的经典实现,它管理完全相同的任务,不需要std::list中的任何类实例的析构函数来担心将任何指针设置为null。

#1


5  

There is no facility in the C++ language to automatically set some pointer somewhere to null, when an object gets destroyed. If some pointer needs to be set to null, somewhere, it is the destructor's responsibility to make it happen. Presumably, in some fashion you need to implement some kind of facility that keeps track of pointers to the class instance, and then in the destructor those pointers will be set to null.

在c++语言中,当一个对象被销毁时,不存在自动将某个指针设置为null的功能。如果某个指针需要设置为null,那么发生这种情况是析构函数的责任。假设,在某种程度上,您需要实现某种工具,以跟踪指向类实例的指针,然后在析构函数中,这些指针将被设置为null。

Having said that, I'm confident that you are misinterpreting your assignment. I see no need whatsoever for any pointer to be set to null, when the class instance gets destroyed.

话虽如此,我相信你误解了你的作业。当类实例被销毁时,我认为任何指针都不需要设置为null。

In your professor's immutable driver, the call to the pop_front() method is going to remove the node from its linked list completely, and return the pointer to the removed node. Your implementation of pop_front() is going to completely remove the node from the list, updating all list pointers accordingly, to point to the remaining elements in the list.

在教授的不可变驱动程序中,对pop_front()方法的调用将完全从其链表中删除节点,并返回指向已删除节点的指针。pop_front()的实现将完全从列表中删除节点,相应地更新所有列表指针,以指向列表中的其余元素。

At this point, no other pointers to the class instance should exist, except for the pointer being returned from pop_front(), and this pointer then immediately gets deleted. There won't be any pointers to your class instance that need to be set to null.

此时,除了pop_front()返回的指针之外,不应该存在指向类实例的其他指针,然后立即删除该指针。不会有任何指向类实例的指针需要设置为null。

Your professor's code is the classical implementation of std::list, which manages the exact same task without requiring the destructors of any class instance in std::list to worry about setting any pointer to null.

您的教授的代码是std::list的经典实现,它管理完全相同的任务,不需要std::list中的任何类实例的析构函数来担心将任何指针设置为null。