I've been trying to come up with a copy constructor for a tree. I've found quite a few suggestions.
This one interested me.
class TreeNode
{
int ascii;
TreeNode* left;
TreeNode* right;
public:
TreeNode() { ascii = 0; left = right = 0; }
TreeNode* clone();
// ...
};
TreeNode* TreeNode::clone()
{
if (TreeNode* tmp = new TreeNode)
{
tmp->ascii = ascii;
if (left) tmp->left = left->clone();
if (right) tmp->right = right->clone();
return tmp;
}
return 0;
}
What does "if (TreeNode* tmp = new TreeNode)
mean?
Other than that it looks alright. It just doesn't work very well.
Any idea what's wrong with it?
The example above came from this site.