【问题标题】:What should i write inside a destructor of a class我应该在类的析构函数中写什么
【发布时间】:2021-10-31 12:08:05
【问题描述】:

我目前正在学习编程考试,并且应该在其中一个练习中为“BTree”类编写析构函数。下面的代码是一个二叉树。我不知道析构函数的主体应该是什么,因为我还没有正确地学习。我认为 delete 里面应该有一些东西,因为我还在堆上用 new 创建了动态对象。提前致谢。

class BTree
{


public:
    vertex* root;

    BTree()
    {
        root = NULL;
    };

    ~BTree() {
        
    };

    bool isEmpty() { return root == NULL; }

    vertex* Nikita = new vertex("Nikita");
    vertex* Vendor = new vertex("Vendor");
    vertex* faehrt = new vertex("faehrt");
    vertex* nach = new vertex("nach");
    vertex* Alexendria = new vertex("Alexandria");


    void main() {
        root = node(node(create(), Nikita,node(create(), Vendor, create() ))
                    , faehrt,
                    node(create(), nach, node(create(), Alexandria, create() ))
                    );
        cout << empty (right(root)) << endl;
        cout << value(left(root)) << endl << endl;

    };
class vertex{

public:

    int key;
    string data;
    vertex* leftChild;
    vertex* rightChild;
    vertex* parent;
    int height;


    vertex(string data){

        key = ID;
        this->data = data;
        leftChild = NULL;
        rightChild = NULL;
        parent = NULL;
        ID++;
    };

    vertex(){
        key = 0;
        leftChild = NULL;
        rightChild = NULL;
        parent = NULL;
    };

    ~vertex(){

    };
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#include "BTree.h"


int main() {

// Aufgabe 1
    BTree B;

    B.main();

// Aufgabe 2
    BTree C = B;
    C.print();


// Aufgabe 3
    BST D;

    D.main();
    D.print(D.root);
    D.sortvector(); //neu hinzugefügt

// Aufgabe 4
//  D.PrintLayers();
}

【问题讨论】:

  • 欢迎来到 StackOverflow!建议您通过meta.stackoverflow.com/questions/334822/…在这里获得更好的答案。
  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: c++ destructor


【解决方案1】:

什么都没有,你不应该在你的析构函数中写任何东西,并且不要使用new 命令来分配你的成员对象。

使用std::unique_ptr&lt;XXXX&gt; 管理您的记忆,因此您不必这样做。

【讨论】:

  • 这是一个测试考试。我会使用智能指针,但我们的教授希望我们这样做。
【解决方案2】:

如上述帖子所述,您应该释放内存中的对象。我不太确定,但我认为如果您不释放/删除这些对象,它只会随机放置在您的程序中未使用的内存,您可以将其用于其他用途。

解决方案

class BTree
{


public:
    vertex* root;

    BTree()
    {
        root = NULL;
    };

    ~BTree() 
    {
        delete this->Nikita;
        delete this->Vendor;
        delete this->faehrt;
        delete this->nach;
        delete this->Alexendria;

    };

    bool isEmpty() { return root == NULL; }

    vertex* Nikita = new vertex("Nikita");
    vertex* Vendor = new vertex("Vendor");
    vertex* faehrt = new vertex("faehrt");
    vertex* nach = new vertex("nach");
    vertex* Alexendria = new vertex("Alexandria");


    void main() {
        root = node(node(create(), Nikita,node(create(), Vendor, create() ))
                    , faehrt,
                    node(create(), nach, node(create(), Alexandria, create() ))
                    );
        cout << empty (right(root)) << endl;
        cout << value(left(root)) << endl << endl;

    };

也只是一个建议 :) 您可以使用智能指针自动调用当前对象的解构器。示例:

std::unique_ptr<BTree> Instance = std::make_unique<BTree>();

Instance->YourFunction(Args...);

如果我犯了任何错误或错误,我愿意接受建设性的批评:)

【讨论】:

  • 应该是第一个可以是一个cant?
  • 我尝试使用您在解决方案中编写的析构函数来执行此操作,但如果我进入调试模式,它不会删除对象。我对析构函数非常困惑,所以我继续进行其他练习^^。是的,我应该学习如何使用智能指针。我们的教授也告诉我们这更好,但他希望我们在这个练习中这样做。无论如何,感谢您的解决方案:)
【解决方案3】:

尽可能少。

~BTree 需要 delete nikita 和朋友,以及任何其他 vertexs 到 own。请注意,如果您需要一个析构函数,那么您经常(在这种情况下肯定会)需要一个复制构造函数和一个赋值运算符。有关详细信息,请参阅The Rule Of Three (and friends) 上的此页面。

另一方面,vertex 可能不拥有任何资源,并且前面链接中讨论的零规则表明它根本不需要析构函数。

其他有用的阅读帮助;你把它们联系在一起,更好地理解构造函数和析构函数的意义:What is meant by Resource Acquisition is Initialization (RAII)?

【讨论】:

    猜你喜欢
    • 2017-01-11
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2012-07-24
    • 2012-01-07
    • 2021-07-26
    • 2015-08-07
    相关资源
    最近更新 更多