复制构造函数

时间:2013-03-13 11:37:53

标签: c++ copy binary-search-tree nodes

我正在使用二叉搜索树。在这里,我正在编写一个从树中删除项目的函数。在以下代码中:

if(root = NULL)//if there is nothing in the tree
{
    cout<<"the Tree is empty"<<endl;//ouput to the screen
    return;//exit the function
}

bool isFound = false;//tells us if the item is found
Node* tmp = new Node();//declare a temp pointer
Node* tmp2 = new Node();;//declare a temp pointer
tmp* = *root;//assign the pointer to something

它正在调用复制构造函数,但正如我现在所知,我只是复制这样的值:

Node& Node::operator= (const Node& node)
{
    data = node.data;
    left = node.left;
    right = node.right;
    return *this;
}

2 个答案:

答案 0 :(得分:1)

您正在分配指针,以分配您需要的对象

*tmp = *root;

tmproot的类型为Node*; *tmp*root的类型为Node

答案 1 :(得分:0)

if(root = NULL)

将其更改为

if(root == NULL)

这也是错误的:

tmp* = *root;//assign the pointer to something