Array类定义
#include <iostream> using namespace std; template <class elemType> class Array { public: Array(){ cout << "调用构造函数" << endl; }; Array(elemType *temp, int n){ m_size = n; a = new elemType[m_size]; for (int i = 0; i < m_size; i++) { a[i] = temp[i]; } cout << "调用构造函数" << endl; }; void Print(void) { for (int i = 0; i < this->m_size-1; i++) { cout << this->a[i] << " "; } cout << this->a[m_size-1] << endl; } ~Array(){ cout << "调用析构函数" << endl; }; private: int m_size; elemType *a; }; int main() { int temp1[6] = { 1, 2, 3, 4, 5, 6 }; float temp2[10] = { 1.2, 2.2, 3.2, 4.2, 5.2, 6.2 }; Array <int> a(temp1,6); Array <float> b(temp2, 6); a.Print(); b.Print(); //Array <float> b(temp2,6); //cout << "hello" << " world" << endl; return 0; }