C++ How to declare as atribute, a pointer to my own type, using templates -
this problem: have class "nodo" (to make bsts) has array of pointers same type attribute, , have son class "nodoavl" (to make avl trees) have inherit atribute, pointers must point same type, is, point "nodoavl". there way this? maybe code, can explain better:
#include<iostream> using namespace std; template <class t> class nodo { protected: t d; "my_own_type" *h[2] = {null}; // here need pointers own type // nodo has 2 pointers nodo // , nodoavl has 2 pointers nodoavl public: nodo(t dato = t()) { d = dato; } }; template <class t> class nodoavl : public nodo<t> { protected: int alt = 0; public: nodoavl (t dato = t()): nodo<t>(dato) {} int altu () {return alt;} };
i gratefull help.
--- update ---
i have read crtp (thanks some programmer dude) , although didn't understand utility, found solve problem. decided make "nodo" big-father class adding 1 label in template "tn" (type of node), array of pointers declared tn*. created 2 son class: nodobst , nodoavl, each 1 inherit "nodo" labeled respective type of node in template. again code explain better idea.
#include<iostream> using namespace std; template <class t, class tn> class nodo { protected: t d; tn *h[2] = {null}; public: nodo(t dato = t()) { d = dato; } }; template <class t> class nodobst : public nodo<t,nodobst<t>> { protected: public: nodobst (t dato = t()): nodo<t,nodobst<t>>(dato) {} }; template <class t> class nodoavl : public nodo<t, nodoavl<t>> { protected: int alt = 0; public: nodoavl (t dato = t()): nodo<t,nodoavl<t>>(dato) {} int altu () {return alt;} }; int main () { nodoavl<int> * narval = new nodoavl<int>(10); cout << narval->altu(); }
it has worked, want know if it's practice or i'm making future problems. want add more methods father class works array of pointers , of them same in kind of node (ej: update pointers, dereference them, kill them, etc). if had solve problem? lot again!
you can obvious way:
nodo<t>* h[2];
but can using class name alone:
nodo* h[2];
Comments
Post a Comment