I am trying to generate the following general tree:
root
|
----------------
| |
child child
|
----
|
child
|
-------------
| | |
child child child
I am planning to use two classes, one stores a parent node element (Tree class) and the other implements linked list for storing children (Single_linked_list).
My class definition for the Tree class is:
template <class Object>
class Tree
{
private:
Object node_val; // this is stored in node of tree
Single_linked_list< Tree<Object> * > children;
public:
// accessors and mutators
...
}
I just wanted to confirm that the Single_linked_list< Tree<Object> * > children;
should have the *
in it because in c++ that is the way of specifying that it is to be a pointer, pointing the start of the linked_list with all the children?
Please let 开发者_开发技巧me know if my interpretation of that line of code is correct.
If I understand your question, you're wondering what the Single_linked_list class template parameter means.
Your statement:
in c++ [the
*
] is the way of specifying that it is to be a pointer, pointing the start of the linked_list with all the children
is worth discussion. The Single_linked_list
template class manages instances of the type given as the template parameter. The template parameter does not change how the list is used. The managed type identified by the template parameter may be built-in - like a pointer - or could be a class. Whatever the template parameter, I would assume that access to the start of the linked list and functions to traverse the list will be accessed by calling methods on children
e.g.
Single_linked_list< SomeClassOrType > my_list;
putThingsOnList( &my_list );
my_list.goToFirst();
while( !my_list.hasNext() )
{
SomeClassOrType &o = children.getCurrent();
children.goToNext();
}
The first part of your statement quoted above is correct: the *
specifies a pointer type. The second part of your statement is where I disagree: the template parameter is not related to the idea of the start of the linked_list.
I hope you find my answer valuable.
精彩评论