使用无参数类型的类模板
#include<iostream> using namespace std; template <class T, int size=10> class A { T *p; public: A(T* q) { p = new T[size]; for(int i=0;i<size;i++) p[i] = *q++; } void disp() { int i; for(i=0;i<size;i++) cout<<p[i]; cout<<endl; } }; int main() { int a[] = {1,2,3,4,5,6,7,8}; char* b = "abcdefghijkl"; A<int, 5> s1(a); cout<<"a:";s1.disp(); A<char> s2(b); cout<<"b:";s2.disp(); return 0; }