c++ - Recursive copy constructor? -


I am trying to write a copy constructor which takes the node of the tree in the tree, and imitates that node, in which With all his children, and his children, and so on. What do I have here:

  node (constant node and n) {nChild = new node (* (n.nwChild)); NeChild = new node (* (n.nChild)); EChild = new node (* (n.seChild)); WChild = new node (* (n.sChild)); }   

I think the last 4 lines are a little funky. Do I understand?

Your constructor is not exception-protected - the second seems to throws new bad_alloc , then how is it going to be cleansed?

You can easily make your own life easier by changing the code> children field in your favorite smart pointer if you do not have any favorite smart pointer, Present with; -)

[edit: Just seen that you did not give data member definitions, I think they were raw pointers. If they are already smart pointers then apologize.]

The initial time looks a bit weird - how come when all four children on the rhs are in the null, then there is no X / Y / height / width copy Did? Generally those members will be handled by the initialization list. And you should do something special if the any children, instead of all , have tap? At present you do not handle the nodes together with children but all 4 are not. If this is the property of your tree, then every node has 4 children or none, then you do not have to check all 4, but you when you copy a leaf node Need to start different areas. If possible for 1 child in the node, you do not copy that node correctly.

In addition to this, the original argument seems fine to me, which provides meaning that nothing is wrong, it will be reproduced.

Comments