Templates

I was reading through this websites tutorial and I was a little confused by how this program works.

This part here is the main source of my confusion.

Nevermind, I think I understand it. It made more sense to me when I put the template declaration all on one line.

1
2
  mysequence <int,5> myints;
  mysequence <double,5> myfloats;



Here is the full program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// sequence template
#include <iostream>
using namespace std;

template <class T, int N> class mysequence {
    T memblock [N];
  public:
    void setmember (int x, T value);
    T getmember (int x);
};

template <class T, int N> void mysequence<T,N>::setmember (int x, T value) {
  memblock[x]=value;
}

template <class T, int N> T mysequence<T,N>::getmember (int x) {
  return memblock[x];
}

int main () {
  mysequence <int,5> myints;
  mysequence <double,5> myfloats;
  myints.setmember (0,100);
  myfloats.setmember (3,3.1416);
  cout << myints.getmember(0) << '\n';
  cout << myfloats.getmember(3) << '\n';
  return 0;
}
Last edited on
Topic archived. No new replies allowed.