C++ 析构函数、图与递归性

C++ 析构函数、图与递归性,c++,recursion,vector,tree,C++,Recursion,Vector,Tree,我有一个graph对象,我想为此创建一个析构函数。然而,我不太适应递归性,我对自己的数据结构有点迷茫。我将展示所涉及的类和析构函数的开头 class Graph { private : Graph* parent; vector<Graph> child; Board tab; bool seen; public

我有一个graph对象,我想为此创建一个析构函数。然而,我不太适应递归性,我对自己的数据结构有点迷茫。我将展示所涉及的类和析构函数的开头

class Graph {

private :
        Graph*               parent;
        vector<Graph>        child;
        Board                tab;
        bool                 seen;

public :
        Graph(const Board&);
        Graph(const Board&, Graph*);
        ~Graph();
        ...
};

class Board {        
    private :
        int**           tab;
        int             nbline;
        int             nbcolumn;
        Position        emptyspot;

    public  :
        Board();
        Board(int, int, Play&);
        Board(int, int);
        Board(const Board&);
        Board(int, int, ifstream&);
        ~Board();
       ...
};
类图{
私人:
图形*父图形;
媒介儿童;
板选项卡;
布尔看见;
公众:
图形(const Board&);
图(const Board&图*);
~Graph();
...
};
班级委员会{
私人:
int**选项卡;
国际在线;
内塔;
定位空罐;
公众:
董事会();
棋盘(int,int,Play&);
董事会(int,int);
委员会(委员会),;
板(int、int、ifstream&);
~Board();
...
};
Position类仅获得2 int(行和列)。 电路板析构函数工作:

Board::~Board()
{
    for(int i = 0; i < this->nbline; i++) {
        delete tab[i];
    }
    delete tab;
}
Board::~Board()
{
对于(inti=0;inbline;i++){
删除标签[i];
}
删除标签;
}
正如您所猜测的,我想销毁图中的一个节点以及以下所有节点

这是我的恳求:

Graph::~Graph() {        
    while(!child.empty()) {                
        for(vector<Graph>::iterator itr = child.begin; itr != child.end; ++itr) {
            delete child[itr];
        }
    }
}
Graph::~Graph(){
而(!child.empty()){
for(vector::iterator itr=child.begin;itr!=child.end;++itr){
删除子项[itr];
}
}
}
这样我就可以递归地进入我的所有分支,对吗?当我找到一个叶子(向量为空)-如果破坏所有东西,那么父向量中会发生什么

我不知道父级是否会将自己设置为NULL(我不这么认为),并且父级向量内存空间不会未分配,因此条件child.empty()不会满足,对吗

  • 如何以及何时销毁*图形

  • 我会冒堆栈溢出的风险吗

    • 我可以在开始删除的根节点中调用
      vector.erase()
      ,以递归方式销毁所有内容,而不是执行for循环吗

    • 由于许多原因,您的析构函数不正确

    • 您的
      子成员
      可能应该是
      向量
      ,这样您就可以实际
      删除它们了
      
    • 如果
      图形
      有任何子对象,则循环是无限的,因为您从未更改
      子对象
      向量的大小
    • child[itr]
      不是获得迭代器对应的
      Graph*
      的方式,
      *itr
    • begin
      end
      是成员函数,因此需要调用它们
    • 成员可能应该被命名为
      子成员
      ,不是吗
    • 正确的循环应该是:

      for (vector<Graph*>::iterator itr = children.begin(); itr != children.end(); ++itr) {
          delete *itr; // this will recursively call Graph::~Graph() 
                       // on the children, and then free their memory
      }
      
      for(vector::iterator itr=children.begin();itr!=children.end();++itr){
      delete*itr;//这将递归调用Graph::~Graph()
      //对孩子们,然后释放他们的记忆
      }
      
      或者,在C++11中,我们只定义:

      std::vector<std::unique_ptr<Graph>> children;
      
      std::向量子类;
      

      这样我们就可以处理内存清理了。

      子对象是向量还是向量?你有
      vector
      但是你在调用delete,所以我很困惑。这是我的一个错误,它应该是vector,我也必须更改构造函数^^'@Csi:如果你没有C++11功能,请检查。它们基本上是一样的,用C++98实现意味着@DevSolar我没有使用Boost的权利;\uuz;老师很老派:“(@DevSolar-Thx-buddy:-)在人们的建议下,我对这个话题做了修改,现在正确了吗?