One way to do this is to add a separate class for the tree, put the root
pointer there, and add all the common items there as well. Then add a tree
pointer to the node
base class, and also add a constructor argument to point all nodes of the tree back to their tree
object:
class node;
class tree {
node *root;
int multiplier; // e.g. 1000 for meters, 1 for kilometers
}
class node {
protected:
tree *owner;
node *parent; // parent is NULL for the root
node(tree *_owner, node *_parent) : owner(_owner), parent(_parent) {}
};
class branch : public node {
list<node> children;
public:
branch(tree *_owner, node *_parent) : node(_owner, _parent) {}
...
};