template<class T> class SimpleVector { unique_ptr<T []>aptr; int arraysize; public: SimpleVector (int) ; // 建構函式 SimpleVector (const SimpleVector &) ; // 複製建構函式 int size () const{ return arraySize; } T &operator [] (int) ; // 過載[]運算子 void print () const; // 輸出陣列元素 };這個類別範本將把型別 T 的元素儲存在一個動態生成的陣列中。這就解釋了為什麼指向這個陣列底部的指標 aptr 被宣告為 T[] 型別的指標。這裡使用了 unique_ptr 獨佔指標作為 aptr 的型別,因為 SimpleVector 物件將不會和程式的其他部分共用動態分配的陣列。
SimpleVector <double> dTable(10);
在類中定義一個模板類的成員函數是很簡單的,例如,在 SimpleVector 類中即可輕鬆定義 size() 函數。但是,如果要在類的外面定義成員函數,則必須使用模板頭來給成員函數的定義新增字首(該模板頭指定了型別形參的列表),然後在定義中還需要使用類別範本的名稱,後面還要跟著一個型別形參的列表(使用尖括號括起來)。template<class T> T &SimpleVector<T>::operator[](int sub) { if(sub < 0 || sub >= arraySize) throw IndexOutOfRangeException(sub) return aptr[sub]; }在這個定義中,由於作用域解析運算子(::)之前需要類的名稱,所以在該位置新增了 SimpleVector <T>。以下是另外一個範例,它是轉換建構函式的定義:
template<class T> SimpleVector<T>::SimpleVector(int s) { arraySize = s; aptr = make_unique<T[]> (s); for (int count = 0; count < arraySize; count++) aptr[count] = T(); }在該範例中,需要在作用域解析運算子之前有 SimpleVector <T>,但是在作用域解析運算子之後,卻只有 SimpleVector,而沒有 <T>,這是因為在作用域解析運算子後面需要的不是類的名稱,而是成員函數的名稱,在這種情況下恰好是建構函式。
template<class T> SimpleVector<T>::SimpleVector(const SimpleVector &obj) { arraySize = obj.arraySize; aptr = make_unique<T []>(arraySize); for (int count = 0; count < arraySize; count++) aptr[count] = obj[count]; }該函數就不需要將 <T> 附加到 SimpleVector 以表示其引數型別。
//SimpleVector.h #include <iostream> #include <cstdlib> #include <memory> using namespace std; // Exception for index out of range struct IndexOutOfRangeException { const int index; IndexOutOfRangeException(int ix) : index(ix) {} }; template <class T> class SimpleVector { unique_ptr<T []> aptr; int arraySize; public: SimpleVector(int); // Constructor SimpleVector(const SimpleVector &); // Copy constructor int size() const { return arraySize; } T &operator[](int); // Overloaded [] operator void print () const; // outputs the array elements }; template <class T> SimpleVector<T>::SimpleVector(int s) { arraySize = s; aptr = make_unique<T[]>(s); for (int count = 0; count < arraySize; count++) aptr[count] = T(); } template <class T> SimpleVector<T>::SimpleVector(const SimpleVector &obj) { arraySize = obj.arraySize; aptr = make_unique<T[]>(obj.arraySize); for (int count = 0; count < arraySize; count++) aptr[count] = obj[count]; } template <class T> T &SimpleVector<T>::operator[](int sub) { if (sub < 0 || sub >= arraySize) throw IndexOutOfRangeException(sub); return aptr[sub]; } template <class T> void SimpleVector<T>::print() const { for (int k = 0; k < arraySize; k++) cout << aptr [k] << " "; cout << endl; }
//This program demonstrates the SimpleVector template. #include <iostream> #include "SimpleVector.h" using namespace std; int main() { const int SIZE = 10; SimpleVector<int> intTable (SIZE); SimpleVector<double> doubleTable(SIZE); // Store values in the arrays for (int x = 0; x < SIZE; x++) { intTable[x] = (x * 2); doubleTable[x] = (x * 2.14); } // Display the values in the arrays cout << "These values are in intTable:n"; intTable.print(); cout << "These values are in doubleTable:n"; doubleTable.print (); // Use the built-in + operator on array elements for (int x = 0; x < SIZE; x++) { intTable[x] = intTable[x] + 5; doubleTable[x] = doubleTable[x] + 1.5; } // Display the values in the array cout << "These values are in intTable:n"; intTable.print (); cout << "These values are in doubleTable:n"; doubleTable.print (); // Use the built-in ++ operator on array elements for (int x = 0; x < SIZE; x++) { intTable[x]++; doubleTable[x]++; } // Display the values in the array cout << "These values are in intTable:n"; intTable.print (); cout << "These values are in the doubleTable: n"; doubleTable.print(); cout << endl; return 0; }程式輸出結果:
These values are in intTable:
0 2 4 6 8 10 12 14 16 18
These values: are in doubleTable:
0 2.14 4.28 6.42 8.56 10.7 12.84 14.98 17.12 19.26
These values are in intTable:
5 7 9 11 il3 15 17 19 21 23
These values are in doubleTable:
1.5 3.64 5.78 7.92 10.06 12.2 14.34 16.48 18.62 20.76
These values are in intTable:
6 8 .10 12 14 16 18 20 22 24
These values are in the doubleTable:
2.5 4.64 6.78 8.92 11.06 13.2 15.34 17.48 19.62 21.76