我需要在这个类的析构函数中写什么吗?

Do I need to write anything in the destructor of this class?

本文关键字:析构函数 什么      更新时间:2023-10-16

谢谢大家!现在我改变了我的逻辑。因为如果我包含相同的指针指向它自己,它将创建无限循环。那么对于这个修改后的函数,我需要写析构函数吗?

#include <stdio.h>
#include <stdlib.h>
#include <tr1/array>
using namespace std;
class Graphnode {
public:
    std::tr1::array<int, 16> state;
    int x;
    int depth;
    Graphnode(std::tr1::array<int, 16>,int,int);
    Graphnode();
    //~Graphnode();
};
Graphnode::Graphnode()
{
    int i=0;
    for(i=0;i<16;i++)
    {
       state[i] = 0;
    }
    x = 0;
    depth = 0;
}
Graphnode::Graphnode(std::tr1::array<int, 16> _state,int _x,int _d)
{   
    int i=0;
    for(i=0;i<16;i++)
    {
       state[i] = _state[i];
    }
    x = _x;
    depth = _d;
}
/*Graphnode::~Graphnode()
{
}*/

当您尝试创建一个Graphnode

时,您的代码将创建一个无限循环

您可以尝试为子Graphnode创建一个特定的构造函数,以便在该构造函数中不会创建更多的Graphnode