析构函数释放分配给矩阵的动态内存

时间:2019-01-22 21:57:45

标签: c++ destructor dynamic-memory-allocation

析构函数释放分配给矩阵的动态内存

〜UTM(){…}

适当地为稀疏矩阵对象释放任何动态分配的内存。

矩阵的创建已经实现,我坚持如何通过另一个类的析构函数删除分配给单独类中新矩阵的内存。

struct node
{
    int data;
    node *next;
};


class UTM
{
public:
    int row, col;
    string name;
    node *head, *tail;

    UTM(string name)
    {
        this->name = name;

        head = NULL;
        tail = NULL;
    }

    UTM(int row, int col, string name)
    {
        this->row = row;
        this->col = col;
        this->name = name;

        head = NULL;
        tail = NULL;
    }

    ~UTM()
    {
        delete sumMatrix;
    }


    UTM *UTM::sumUTM(UTM &other)
    {
      UTM *sumMatrix = new UTM(row, col, "Addition Matrix Result");

      node *temp1 = other.head;
      node *temp2 = this->head;

      while(temp2 != NULL)
      {
        int sum = temp1->data + temp2->data;
        sumMatrix->inputNode(sum);

        temp1 = temp1->next;
        temp2 = temp2->next;
      }

      return sumMatrix;
    }

我如何通过析构函数释放分配的内存,我只停留在这部分上。

0 个答案:

没有答案