C++: Duplicating a tree of derived elements
问题 I have a base class and several derived classes. The base class looks like this: class Base { int type; //the derived type the object belongs to int nOfChildren; Base** children; //each child can be any of the derived types ... } Now I need to duplicate an instance of Base . Because of the recursion, a virtual method Base::duplicate() is needed. It also seems clear what should go in it: Base temp = new Base(); temp->type = temp; temp->nOfChildren = nOfChildren; temp->children = new Base*