Amit Bhatia said:
Thanks.
but this brings me to one more question: I guess the elements of a list
"should" also be default constructible?
yes, or you have to allocate with initialization
I want to make a tree of nodes as follows, but am not sure now if it
will work:
In node.h
class Node
{
// some stuff ctors, dtors, etc;
//Default ctor;
Node &parent_node; // Could use pointer here but want to avoid it if
possible.
vector< Node & > child_nodes; //Could use pointer here but want to
avoid it if possible.};
No no no... cannot do a vector of references. This is illegal
you must use pointers here.
Are you sure you want to use vector here...
If you have to do a lot of insertion/removal list is probably the
structure you want.
In tree.h
class Tree
{
//again ctors, dtors, and some other stuff;
list< Node &> treeofnodes;
Samething here with list.. must use pointers.
I dont see why you are using a list here and a vector in
your node class
};
I don't want to create duplicate copies of nodes, and want everything to
point correctly to elements in the global list treeofnodes.
any suggestions?
Well if you want a basic implementation of a tree, you seems to be on the
right track. just have a tree class with a list of Node. And Node having
also
a list of Node. Now do you needs your Nodes to be dynamically allocated?
something like
class Tree
{
public:
[...] ctor dtor
[...] // probably want to give access to some functions to search or sort
your tree
private:
Node m_Root;
list<Node> m_Childs;
};
class Node
{
public:
[...] ctor dtor
private:
[...] info about the node
list<Node> m_Childs;
// Do you really need a reference to the parent??? maybe not...
};