1: template <typename T, int nSize> // nSize is the expression parameter
2: class Buffer
3: {
4: private:
5: // The expression parameter controls the size of the array
6: T m_atBuffer[nSize];
7:
8: public:
9: T* GetBuffer() { return m_atBuffer; }
10:
11: T& operator[](int nIndex)
12: {
13: return m_atBuffer[nIndex];
14: }
15: };
16:
17: int main()
18: {
19: // declare an integer buffer with room for 12 chars
20: Buffer<int, 12> cIntBuffer;
21:
22: // Fill it up in order, then print it backwards
23: for (int nCount=0; nCount < 12; nCount++)
24: cIntBuffer[nCount] = nCount;
25:
26: for (int nCount=11; nCount >= 0; nCount--)
27: std::cout << cIntBuffer[nCount] << " ";
28: std::cout << std::endl;
29:
30: // declare a char buffer with room for 31 chars
31: Buffer<char, 31> cCharBuffer;
32:
33: // strcpy a string into the buffer and print it
34: strcpy(cCharBuffer.GetBuffer(), "Hello there!");
35: std::cout << cCharBuffer.GetBuffer() << std::endl;
36:
37: return 0;
38: }