简体   繁体   English

我应该如何为 C++ 中的节点 class 定义我的析构函数?

[英]How should I define my destructor for the Node class in C++?

I am supposed to implement a class of Nodes for the tree that consists of static nodes (for educational purposes).我应该为由 static 节点组成的树实现一个 class 节点(用于教育目的)。 The classes headers look like this:类标题如下所示:

class CNodeStatic
{
private:
    int i_val;
    CNodeStatic *pc_parent_node;
    vector<CNodeStatic> v_children;
public:
    CNodeStatic() {i_val =0; pc_parent_node = NULL;};
    ~CNodeStatic();
    void vSetValue(int iNewVal) {i_val = iNewVal;};
    int iGetChildrenNumber() {return (v_children.size());};
    void vAddNewChild();
    void vAddChild(CNodeStatic pcChildNode);
    CNodeStatic *pcGetChild (int iChildOffset);
};
class CTreeStatic
{
private:
    CNodeStatic c_root;
public:
    CTreeStatic();
    ~CTreeStatic();
    CNodeStatic *pcGetRoot() {return (&c_root);};
    bool bMoveSubtree(CNodeStatic *pcParentNode, CNodeStatic *pcNewChildNode);
    void vPrintTree();
};

However I am not sure how the destructor for such class should look like.但是我不确定这种 class 的析构函数应该是什么样子。 I know that we need to define destructors when there is a dynamically allocated memory or pointer in class.我知道当有动态分配的 memory 或 class 中的指针时,我们需要定义析构函数。 In this case it's pc_parent_node that points to the parent of the node.在这种情况下,它是pc_parent_node指向节点的父节点。 However if I try to define my destructor only as delete pc_parent_node the program won't work.但是,如果我尝试将我的析构函数仅定义为delete pc_parent_node该程序将无法运行。

I know that we need to define destructors when there is a dynamically allocated memory or pointer in class我知道当存在动态分配的 memory 或 class 中的指针时,我们需要定义析构函数

Right.正确的。 If a class manages a resource, it must manage the resource and cleaning up is part of that.如果 class 管理资源,它必须管理资源并且清理是其中的一部分。

In this case it's pc_parent_node that points to the parent of the node.在这种情况下,它是pc_parent_node指向节点的父节点。

Pointers.= managing a resource, In particular. Pointers.= 管理资源,特别是。 raw pointers should not be used to manage lifetime.不应该使用原始指针来管理生命周期。

The shown code does not dynamically allocate something.显示的代码不会动态分配某些东西。 The class does not seem to own a resource, hence there is nothing it must delete in its destructor. class 似乎不拥有资源,因此它的析构函数中没有任何内容必须删除。 If it does manage a resource, it should do so by using a smart pointer or container.如果它确实管理资源,它应该通过使用智能指针或容器来做到这一点。

Read about the rule of zero ( https://en.cppreference.com/w/cpp/language/rule_of_three ).阅读零规则 ( https://en.cppreference.com/w/cpp/language/rule_of_three )。 A class that manages a resource should do only that and nothing else.管理资源的 class 应该只做那个,而不是别的。 In all other cases you should strive to follow the rule of zero by delegating lifetime managment to smart pointers or containers.在所有其他情况下,您应该通过将生命周期管理委托给智能指针或容器来努力遵循零规则。 The best destructor is a destructor that the compiler can generate.最好的析构函数是编译器可以生成的析构函数。 Only if that is not sufficient you need to manually manage something.仅当这还不够时,您才需要手动管理某些内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM