User-Agent: OSXnews 2.081
Xref: number1.nntp.dca.giganews.com comp.lang.c++:817485
"Eric Pruneau" <er**********@cgocable.cawrote:
>
"Amit Bhatia" <am****************@nospam.gmail.coma écrit dans le message
de news: ec**********@zinnia.noc.ucla.edu...
>>
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<Nodem_Childs;
};
class Node
{
public:
[...] ctor dtor
private:
[...] info about the node
list<Nodem_Childs;
// Do you really need a reference to the parent??? maybe not...
};
Thanks. Yes I do need quick access to the parent. In your suggestion,
there is something like
list<Nodem_childs;
But won't this result in duplicate copies: one in node and one in tree?
I want to avoid that. The feature that I wish to have is to be able to remove a particular
child node whenever I want, and also free up that space from the tree. If
I use pointers, and I have a list of pointer
class Tree{
//; ;
list<Node * m_Childs;
};
but when I free memory from a particular pointer, can I ensure somehow
that that pointer entry is also removed from the list? I guess I could do
it if I could use iterators as
class Node{
//; ;
vector<list<Node>::iterator m_Childs;
};
and then
class Tree{
//; ;
list<Nodetree;
};
but just using pointers ? btw, I wanted to use vector in nodes as I will
be needing random access, and when I do have to delete the children, I
will probably be requiring to delete all of them in one go.
thanks,
--a.