title: C++ How to declare as atribute, a pointer to my own type, using templates
body: <p>This is my problem: I have a class "Nodo" (to make BSTs) which has an array of pointers to its same type as attribute, and then I have a son class "NodoAvl" (to make AVL Trees) which have to inherit that atribute, but the pointers must point to its same type, that is, to point to a "NodoAvl". There is any way to do this?  Maybe with my code, I can explain it better:</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  template &lt;class T&gt;
  class Nodo
   {
   protected:
    T d;
    "my_own_type" *h[2] = {NULL};  // Here I need pointers to my own type 
                                   // so Nodo has 2 pointers to Nodo
                                   // and NodoAvl has 2 pointers to NodoAvl
   public:
    Nodo(T dato = T()) { d = dato; }
   };
  
  template &lt;class T&gt;
  class NodoAvl : public Nodo&lt;T&gt;
   {
   protected:
    int alt = 0;
   public:
    NodoAvl (T dato = T()): Nodo&lt;T&gt;(dato) {}
    int altu () {return alt;}
   };
  </code></pre>
  
  <p>I will be gratefull for your help.</p>
  
  <hr>
  
  <p>--- UPDATE ---</p>
  
  <p>I just  have read about CRTP (thanks <em>Some programmer dude</em>) and although I didn't understand all its utility, I found that it could solve my problem. I decided to make "Nodo" a big-father class adding one label in its template "TN" (type of node), so my array of pointers could be declared as TN*. Then I created 2 son class: NodoBst and NodoAvl, each one inherit from "Nodo" but labeled with its respective type of node in the template. Again my code will explain better my idea.</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  template &lt;class T, class TN&gt;
  class Nodo
   {
   protected:
    T d;
    TN *h[2] = {NULL};
   public:
    Nodo(T dato = T()) { d = dato; }
   };
  
  template &lt;class T&gt;
  class NodoBst : public Nodo&lt;T,NodoBst&lt;T&gt;&gt;
   {
   protected:
   public:
    NodoBst (T dato = T()): Nodo&lt;T,NodoBst&lt;T&gt;&gt;(dato) {}
   };
  
  template &lt;class T&gt;
  class NodoAvl : public Nodo&lt;T, NodoAvl&lt;T&gt;&gt;
   {
   protected:
    int alt = 0;
   public:
    NodoAvl (T dato = T()): Nodo&lt;T,NodoAvl&lt;T&gt;&gt;(dato) {}
    int altu () {return alt;}
   };
  
  int main ()
   {
   NodoAvl&lt;int&gt; * narval = new NodoAvl&lt;int&gt;(10);
   cout &lt;&lt; narval-&gt;altu();
   }
  </code></pre>
  
  <p>It has worked, but I want to know if it's a good practice or I'm making future problems. I want add more methods to the father class which works with the array of pointers and all of them are the same in any kind of node (ej: update the pointers, dereference them, kill them, etc). What would you do if you had to solve this problem? Thanks a lot again!</p>