title: C++ tree/forest structure for nodes of different class types
body: <p>\\while implementing a solution found with casting over a a common baseclass</p>
  
  <p>\\ with virtal members.</p>
  
  <p>\\i found out about univeral references, because that is a other question i created a new one:</p>
  
  <p><a href="https://stackoverflow.com/questions/29590429/c-is-it-possible-to-use-universal-references-for-a-tree-forest-with-nodes-that">https://stackoverflow.com/questions/29590429/c-is-it-possible-to-use-universal-references-for-a-tree-forest-with-nodes-that</a></p>
  
  <p>please refer also to this original question.</p>
  
  <p>I want to organize the hierarchical tree structure of objects with different  types. </p>
  
  <p>The solution should do its work at compile time, so i found out that i have to do this with templates at best without any casting.</p>
  
  <p>after some try's that are in the editlog and had some fundamental flaws.</p>
  
  <p>i found a way to split it up in two classes</p>
  
  <ul>
  <li><p>forest that stores all the nodes and does give them two coordinates the indexnumbers in the vector&lt; vector &lt;...> ></p></li>
  <li><p>node a template &lt; T > that stores the T Object and the relationship to the other nodes</p></li>
  </ul>
  
  <p>my idea does not yet have a way to access the memberfunctions of the Objects stored inside the node class, without breaking the unique_pointer that secures the resourcemanagement.</p>
  
  <p>and i would like to know how to ensure typesafty when accessing a object inside the node class.</p>
  
  <p>there may be errors inside the code, i'm quite sure that it will not compile, the question is about the concept.</p>
  
  <p>Questions are inside the comments.</p>
  
  <p>verion 4:</p>
  
  <pre><code> class Forest
   {
    public:
       template&lt;typename T&gt;
       {friend class Node&lt;T&gt;;} \\every Node&lt;T&gt; should have access to the forest
       Forest();
       ~Forest();
       Forest(const Forest&amp;)=delete;  \\does not make any sense to copy or assign the forest to another forest.
       Forest operator=(const Forest&amp;)=delete;
       int insertroot()  \\every tree has a void nullptr seed/root so that the forest does not need to be templatet, returns the treenumber
       {    \\implementation
       if(this-&gt;Nodes.size()==0)
          {
             std::vector&lt;std::unique_ptr&lt;Node&lt;void&gt; &gt; &gt; h0;
             h0.push_back(std::unique_ptr&lt;Node&lt;void&gt;(new Node&lt;void&gt;(nullptr));
             this-&gt;Nodes.push_back(h0);
          }else
          {
               this-&gt;Nodes[0].push_back(std::unique_ptr&lt;Node&lt;void&gt;(new Node&lt;void&gt;(nullptr,this)));
          }
       return this-&gt;Nodes[0].size()-1;
  
       }
       Node&lt;void&gt;* getroot(int i)   \\ to later allow access to the children and the Objects inside them
    {
       if(Nodes.size&gt;0){
        if((i&gt;0)&amp;(i&lt;Nodes[0].size()))
        {
            return Nodes[0][i].get();
        }
    }
    private:
        std::vector&lt;std::vector&lt;unique_ptr&lt;Node&lt;void&gt; &gt; &gt; nodes; \\is it possible to fill this vector with any Node&lt;T&gt;? its a vector*2 to a unique_ptr to a classpointer with a member pointer to any class. from what i read about templates they create a extra class  for every type, so basicly the unique_ptr have a different type and i cannot store different types in a vector without casting?
   }
  
   template&lt;typename T&gt;
   class Node
   {
     public:
        Node(T n,Forest * Fo) \\ every Node is in the forest and has access to the other nodes and forest information
        :object(std::unique_ptr(n)),F(Fo)
        {
            if(n==nullptr)
            {    
                 this-&gt;lvl=0;
                 this-&gt;place=F-&gt;Node[0].size();
                 this-&gt;parent=-1;
            }
        }
        ~Node();
        Node(const Node&amp;)=delete;
        Node operator=(const Node&amp;)=delete;
        T getObject(){return object.get();} \\how does the compiler know the type? see getchild 
  
        template&lt;typename C&gt;
        {
           Node&lt;C&gt; * getchild(int){}   \\not yet exsisting implementation of get child[int] how do i teach the compiler what int responds to what type?
                                       \\when i understand templates correct then Node&lt;C&gt; are different Classes for every C??
          addChild(C c)
          {
             Node * n=new Node(c,this-&gt;F);
             n-&gt;parent=this-&gt;place;
             n-&gt;lvl=this-&gt;lvl+1
             if(F-&gt;nodes.size()&lt;=n-&gt;lvl)
             {
                n-&gt;place=0;
                h0=std::vector&lt;unique_ptr&lt;Node&lt;C&gt;&gt; &gt;;  
                h0.push_back(unique_ptr&lt;Node&lt;C&gt;(n))
                F-&gt;Nodes.push_back(h0); \\are vector&lt;uniptrNode&lt;C&gt; &gt; and vector&lt;uniptrNode&lt;void&gt;&gt; compatible?
             }else
             {
                n-&gt;place=F-&gt;nodes[n-&gt;lvl].size();
                F-&gt;Nodes[n-&gt;lvl].push_back(unique_ptr&lt;Node&lt;C&gt; &gt;(n));
             }
             this-&gt;children.push_back(c-&gt;place);
          } 
        }        
     private:
       int parent,place,lvl;
       std::vector&lt;int&gt; children;
       unique_ptr&lt;T&gt; object;
       Forest * F;
   }
  </code></pre>
  
  <p>does anyone know a way to implement a container like this?
  maybe there is some abstract type type i did not find out about, so do that i can add a method type getnodetype(int) or checknodetype(int,type), could i assign this with auto nodex=y->getObject()? But then how does the compiler know what methodes nodex has and does not has?</p>
  
  <p>EDIT: i removed the original Post because v4 is very close to a working solution version 1-3 should be in the editlog</p>