我有一个小问题...... 我有这个模板类,它创建一个二维数组:
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];
for (unsigned i = 0; i < P; i++) {
mat[i] = new T*[tam];
for (unsigned j = 0; j < P; j++) {
mat[i][j] = dato;
}
}
}
template <class T, unsigned P>
mapa<T, P>::~mapa() {
for (unsigned i = 0; i < tam; i++) {
delete mat[i];
}
delete mat;
}
template <typename T, unsigned P>
T &mapa<T, P>::operator ()(unsigned i, unsigned j) {
/*if (i < 0 || i >= 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];
for (unsigned i = 0; i < P; i++) {
mat[i] = new T*[tam];
for (unsigned j = 0; j < P; j++) {
mat[i][j] = orig[i][j];
}
}
return mat;
}
我想在下一堂课中使用它:
class museo
{
int sizeStep;
mapa<objetoMuseo*, 50> mapaMuseo;
public:
museo();
~museo();
void visualizar();
};
我应该如何使用museo.cpp中的构造函数初始化模板对象?
答案 0 :(得分:1)
您可以使用初始化列表初始化字段。
museo::museo()
: sizeStep(0),
mapaMuseo(???)
{
}
当然在哪里???你应该添加一个对objectoMuseo指针的引用,因为这是构造函数所要求的。