I have a Tree_item class (C++/Qt4 is used, C++03 compatible), which represents a tree structure. Each node has a raw pointer to parent and QList of pointers to children. Tree_item has a copy constructor, which is called recursively and works fine.
Tree_item::Tree_item(const Tree_item &tree_item)
{
//deep copy tree_item members to the current instance
//...
for (unsigned int i = 0;i < tree_item.child_list_.size();i++)
{
Tree_item* new_child = new Tree_item(*tree_item.child_list_.at(i));
new_child->parent_item_ = this;
child_list_ << new_child;
}
}
But I need to create an inherited class Extended_tree_item, which contains some int and bool members and methods. This new class must have a constructor with a const reference to the base class instance (additional members must be set to default values). I've heard about Prototype design pattern, but the usage examples are primitive. How should constructors and clone methods be organized for a tree with base and derived classes?
Aucun commentaire:
Enregistrer un commentaire