CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2007
    Posts
    3

    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.

  2. #2
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    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.

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    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.

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured