c++ - initializate template object -
i'm having little question... have template class, make bidimensional array:
template <typename t, unsigned p> class mapa { private: t **mat; unsigned tam; public: mapa(t &dato); mapa(const mapa& orig); virtual ~mapa(); mapa<t, p> &operator=(const mapa<t, p> &orig); t &operator()(unsigned i, unsigned j); }; template <class t, unsigned p> mapa<t, p>::mapa(t &dato) { tam = p; mat = new t**[tam]; (unsigned = 0; < p; i++) { mat[i] = new t*[tam]; (unsigned j = 0; j < p; j++) { mat[i][j] = dato; } } } template <class t, unsigned p> mapa<t, p>::~mapa() { (unsigned = 0; < tam; i++) { delete mat[i]; } delete mat; } template <typename t, unsigned p> t &mapa<t, p>::operator ()(unsigned i, unsigned j) { /*if (i < 0 || >= tam) { throw errorderango("posición de memoria inexistente."); } if (j < 0 || j >= tam) { throw errorderango("posición de memoria inexistente."); } if (k < 0 || k >= tam) { throw errorderango("posición de memoria inexistente."); } return mat[i][j][k];*/ } template <typename t, unsigned p> mapa<t, p> &mapa<t, p>::operator=(const mapa<t, p> &orig) { tam = orig.tam; mat = new t**[tam]; (unsigned = 0; < p; i++) { mat[i] = new t*[tam]; (unsigned j = 0; j < p; j++) { mat[i][j] = orig[i][j]; } } return mat; }
i want use in next class:
class museo { int sizestep; mapa<objetomuseo*, 50> mapamuseo; public: museo(); ~museo(); void visualizar(); };
how should make constructor in museo.cpp initializate template object?
you can initialize fields using initialization lists.
museo::museo() : sizestep(0), mapamuseo(???) { }
where of course between ??? should add reference objectomuseo pointer constructor calls for.
Comments
Post a Comment