array template : generic class « template « C++ Tutorial






#include <iostream.h>

template <class T, int N>
class array {
    T memblock [N];
  public:
    setmember (int x, T value) {
       memblock[x]=value;
       return 0;
    }
    T getmember (int x) {
       return memblock[x];
    }
};

int main () {
  array <int,5> myints;
  array <float,5> myfloats;
  
  myints.setmember (0,100);
  
  myfloats.setmember (3,3.1);
  
  cout << myints.getmember(0) << '\n';
  
  cout << myfloats.getmember(3) << '\n';
  
  return 0;
}
25
0








13.12.generic class
13.12.1.A generic class
13.12.2.Operator overload for generic parameters
13.12.3.Class specialization
13.12.4.array template
13.12.5.Use two generic data types in a class definition