简体   繁体   English

C++中N叉树的复制构造函数

[英]Copy constructor of N-ary tree in C++

I have the following header class:我有以下 header class:

class Tree
{
private:
    string label;
    vector<Tree*> children;
    void free(Tree* tree);
    Tree* copyFrom(const Tree* other);

public:
    Tree(string _label, vector<Tree*> _children= vector<Tree*>());
    Tree(const Tree& other);
    void addChild(Tree* child);
    Tree& operator=(const Tree& other);
    Tree* addChild(Tree* parent, string label);
    void removeChild(vector<Tree*>::iterator position);
    void insertTree(Tree* tree, vector<Tree*>::iterator position);
    ~Tree();
};

and following implementation和以下实施

#include "Tree.h"

string Tree::getLabel() const
{
    return label;
}

Tree* Tree::copyFrom(const Tree* other)
{
    if (other == nullptr)
    {
        return nullptr;
    }
    Tree* result = new Tree(other->label);
    vector<Tree*> _children;
    for (int i = 0; i < other->children.size(); i++)
    {
        _children.push_back(copyFrom(other->children[i]));
    }
    result->children= _children;
    
    return result;
}

void Tree::free(Tree* tree)
{
    if (this == nullptr)
    {
        return;
    }
    for (int i = 0; i < tree->children.size(); i++)
    {
        delete tree->_children[i];
    }
    tree->children.clear();
    
}

Tree::Tree(string _label, vector<Tree*> _children)
    :label(_label), children(_children)
{

}

Tree::Tree(const Tree& other)
{
    const Tree* pointer = &other;
    copyFrom(pointer);
    this->label = pointer->label;
    this->children= pointer->children;
    pointer = nullptr;
}




Tree& Tree::operator=(const Tree& other)
{
    if (this != &other)
    {
        free(this);
        const Tree* pointer = &other;
        copyFrom(pointer);
        this->label = pointer->label;
        this->children= pointer->children;
        pointer = nullptr;
    }
    return *this;
}



Tree::~Tree()
{
    free(this);
}

void Tree::addChild(Tree* child)
{
    children.push_back(child);
}

Tree* Tree::addChild(Tree* parent, string label)
{
    if (parent == nullptr)
    {
        throw "No parent";
    }
    Tree* result = new Tree(label);
    parent->children.push_back(result);
    return result;
}

void Tree::removeChild(vector<Tree*>::iterator position)
{
    if (children.size() == 0)
    {
        throw "No children to delete from!";
    }
    int index = position - children.begin();
    Tree* toDelete = children.at(index);
    children.erase(position);
    delete toDelete;
}

void  Tree::insertChild(Tree* tree, vector<Tree*>::iterator position)
{
    if (tree== nullptr)
    {
        throw "Tree cannot be null!";
    }
    children.insert(position, tree);
}

I have some struggles with my destructor, copy constructor and consequently, operator=.我对我的析构函数、复制构造函数和因此的 operator= 有一些挣扎。 I believe my destructor works correctly, but my copy constructor is giving me a hard time, because it is supposed to create a deep copy, but when I copied an object, let's call the copy copyTree and the original firstTree, and then removed a child from firstTree, the change affected copyTree as well.我相信我的析构函数工作正常,但我的复制构造函数让我很难过,因为它应该创建一个深拷贝,但是当我复制一个 object 时,让我们调用副本 copyTree 和原始的 firstTree,然后删除一个孩子从 firstTree 开始,更改也影响了 copyTree。 What am I doing wrong?我究竟做错了什么?

The replies provided me with great workarounds for this hassle, but to answer my question, the concrete problem was that I forgot to assign the result from copyFrom, that is:回复为我提供了解决这个麻烦的很好的解决方法,但要回答我的问题,具体问题是我忘记分配 copyFrom 的结果,即:

Box::Box(const Box& other)
{
    const Box* pointer = &other;
    Box* result = copyFrom(pointer);
    this->label = result->label;
    this->boxes = result->boxes;
    this->souvenirs = result->souvenirs;
    pointer = nullptr;
}

and same for operator =和运算符一样=

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

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