Topic: Is this valid C++ code ? Please help.


Author: abed@saturn.wustl.edu (Abed M. Hammoud)
Date: 2 Dec 1993 17:14:53 GMT
Raw View
Is this a valid C++ code. I would like to use some thing like that
so I can define a class that suppost multidimesnsional arrays.

Thanks,

I am using g++ 2.5.3 on a sun-sparc station.
-----------------------------------------------------------------------------
#include <iostream.h>

template <class T, int dim>
class A {
public:
   T getSomthing();
   A(int);
   ~A();
private:
   int xxx;
};

template <class T, int dim>
A<T, 1>::A(int x) : xxx(x) {cout << "Hello this is 1" << endl; }

template <class T, int dim>
A<T, 2>::A(int x) : xxx(x) {cout << "Hello this is 2" << endl; }

template <class T, int dim>
A<T, 1>::~A() {cout << "Bye this is 1" << endl; }

template <class T, int dim>
A<T, 2>::~A() {cout << "Bye this is 2" << endl; }

template<class T, int dim>
T A<T, 1>::getSomething() { return xxx; }

template<class T, int dim>
T A<T, 2>::getSomething() { return xxx * xxx; }

main() {

   A<float, 1> BB(1);
   cout << BB.getSomething() << endl;

   A<double, 1> CC(1);
   cout << CC.getSomething() << endl;

}