大佬教程收集整理的这篇文章主要介绍了C++类模板用法详解,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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
以上是大佬教程为你收集整理的C++类模板用法详解全部内容,希望文章能够帮你解决C++类模板用法详解所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。