Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another Templated Class Question

Status
Not open for further replies.

Zaid

Programmer
Jul 21, 2000
6
CA
I have a templated class:<br><br>template &lt;class ANYTYPE&gt;<br>TREE {<br>public:<br>...<br>private:<br>&nbsp;struct Node {<br>&nbsp;&nbsp;ANYTYPE data;<br>&nbsp;&nbsp;Node * Right;&nbsp;&nbsp;// Pointer to Right Child Node<br>&nbsp;&nbsp;Node * Left;&nbsp;&nbsp;&nbsp;// Pointer to Left Child Node<br>&nbsp;};<br>...<br>};<br><br>The tree is basically a set of pointers, either pointing to a node in the tree or null. It is a binary tree, so each node has at most two children.<br><br>One member function, TreeMax, returns the largest valued node in the tree. Now since this is a templated class, this function returns the ANYTYPE data type. This is not a problem when the tree actually contains nodes. However, when the tree is empty (i.e. just one pointer pointing to null) I run into trouble. I can return null for any data type, but if the program user decides to do something like this:<br><br><br>TREE&lt;char *&gt; Animals; // Tree is empty<br>cout &lt;&lt; Animals.TreeMax();<br><br>Here I run into trouble, because TreeMax is null, and I can't print null to the screen (I get an error). So, my question is, is there any variable I can substitue for null that can be printed for any data type? In other words, what should I make TreeMax return in the case that the tree is empty (one pointer pointing to null)? <br><br>Thanks.<br><br>Zaid<br>
 
&gt; One member function, TreeMax, returns the largest valued node in the tree<br><br>Show us the TreeMax() signature.<br><br>If it is: <br>NODE* TreeMax();<br><br>Then <br>&gt; cout &lt;&lt; Animals.TreeMax();<br><br>makes no sense in any case as it merely displays the address of the pointer. But, if a user wanted to display the address he/she should do this as always when dealing with pointers:<br><br>if ( Animals.TreeMax())<br>&nbsp;&nbsp;cout &lt;&lt; Animals.TreeMax();<br><br>&quot;But, that's just my opinion... I could be wrong&quot;.<br>-pete
 
TreeMax is actually defined as follows - <br><br>ANYTYPE TreeMax();<br><br>My problem is that I don't know what to return in the case where the tree is empty, because ANYTYPE could be char, int, float, etc. Also, I'm trying to avoid having the user check that the tree is empty first.<br><br>Zaid
 
Dear Zaid,<br><br>&gt; My problem is that I don't know what to return in the case where the tree is empty<br><br>Obviously you need a 'type' that will help you solve this problem and ANYTYPE is not going to work. So you need to change the function signature to begin with.<br><br>&gt; Also, I'm trying to avoid having the user check that the tree is empty first.<br><br>That is almost illogical. Let's say that the user implements your template with 'int'. So you decide to return a '0' when the tree is empty. How does the user determine that the value is zero because the tree is empty rather than that is just the maximum value in the tree?<br><br>Anyway you might consider returning an Iterator from your TreeMax() function. The user would still need to check it against 'end', but that is a normal convention for template implementations.<br><br>&quot;But, that's just my opinion... I could be wrong&quot;.<br>-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top