Topic: How to nest a non-template class in a template class ?
Author: abed@saturn.wustl.edu (Abed M. Hammoud)
Date: 4 Dec 1993 18:27:49 GMT Raw View
Hello, the title says it all. I cannot seem to get the code below
to work. Please let me know what you think. (the non-template version
works fine.) I am using g++ 2.5.5 and libg++2.5.1 on a sun sparc station.
Thanks,
------------------------------cut here-----------------------------
#include <iostream.h>
template <class T>
class array { // <----template class.
class shape { // <----non-template nested class.
public:
shape(int, int);
~shape();
int operator == (const shape&) const;
int operator != (const shape&) const;
shape operator / (int) const;
shape operator * (int) const;
int r, c;
};
public:
array() : s(0), p(0) {}
array(int x, int y) : s(new shape(x, y)) {}
~array() {}
int getNrow() {return s->r; }
int getNcol() {return s->c; }
shape getDim() {return *s; }
private:
T *p;
shape *s;
};
template <class T>
array<T>::shape::shape(int x, int y) : r(x), c(y) {}
template <class T>
array<T>::shape::~shape() {}
template <class T>
int array<T>::shape::operator == (const array<T>::shape& A) const {
return ((r == A.r) && (c == A.c));
}
template <class T>
int array<T>::shape::operator != (const array<T>::shape& A) const {
return ((r != A.r) || (c != A.c));
}
template <class T>
array<T>::shape array<T>::shape::operator / (int x) const {
return array<T>::shape(r / x, c / x);
}
template <class T>
array<T>::shape array<T>::shape::operator * (int x) const {
return array<T>::shape(r * x, c * x);
}
main() {
array<float> B(5, 3);
array<float> C(5, 3);
array<int> D(3, 5);
if (B.getDim() == C.getDim())
cout << "C and B are of same dim" << endl;
if (!(B.getDim() == D.getDim()))
cout << "B and D are not of same dim" << endl;
}