|
-
August 20th, 2007, 01:53 PM
#1
pointer type for templated node class
I am trying to make a robust Node<K,D> class, where K is the key type, and D is the datatype. I don't know what to do for the child pointers.
I would like to be able to do this:
Node<int,char> node_one(1,'n');
Node<char,bool> node_two('m',true);
and then set a node_one.children[LEFT] pointer to the address of node_two. However, if I declare the children pointer as:
Node<K,D>* children[2];
then node_one's pointers can only point to Node<int,char> objects, whereas I want to point to a Node<char,bool> object, node_two.
What to do?
Thank you.
-
August 20th, 2007, 02:52 PM
#2
Re: pointer type for templated node class
You could have something along the lines of Node<K, D, K_Left = K, K_Right = K>.
You could also have Node<K, D> be a subclass of NodeBase<D>, and have the child pointers be of type NodeBase<D> *, but somehow I doubt this would fit well into a usable design.
Depending on what you need the structure to be capable of, the solution could get very tricky and not nearly as elegant as you'd like. Using something like boost::any as a key type might end up being the lesser of all possible evils.
Last edited by Hermit; August 20th, 2007 at 02:58 PM.
-
August 20th, 2007, 03:25 PM
#3
Re: pointer type for templated node class
I would suggest to have an untemplated abstract base class from which your templated Node class(es) inherit(s).
But it is difficult to grasp, what you are trying to implement here. If my key is of type x, and the child key is of type y, how do you know if to put the child on the left or on the right.
-
August 21st, 2007, 01:29 AM
#4
Re: pointer type for templated node class
What you are practically asking for is:
Code:
struct Node
{
boost::any Key;
boost::any Value;
boost::any* LeftChild;
boost::any* RightChild;
};
Because if you are trying to make node_two as child of node_one, there is no way to know what type node_two that the node pointer should be declared as from just the key type and value type. It could be just about anything. For node one you used int and char and for node two you used char and bool. So, what do you do? Have 2 template arguments, 3 template arguments or 4? Without having 4, it is not possible and such node classes seem useless to me where a totally different node object is connecting itself to a totally different another node via the two child pointers.
Why not just group similar types to each other? Just have type for key and value for node like this:
Code:
template<typename Key, typename Value>
struct Node
{
Key key;
Value value;
Node<Key, Value>* LeftChild;
Node<Key, Value>* RightChild;
};
Will this not suffice?
What do you mean by making a "robust" node class?
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|