窗口模板没做过给你个类模板
#include<iostream.h>template<class C>
void square(C num,C *result)
{
*result=num*num;
}
template<class T>
class Array
{
private:
int length;
T *array;
public:
int getlength()
{
return length;
}
T & operator[](int i)
{
return array[i];
}
Array(int l)
{
length=l;
array=new T[length];
}
~Array()
{
delete []array;
}
};
int main()
{
int i,j=3;
square(j,&i);
double d, e=4.1;
square(e,&d);
Array <int> iarray(5);
for (i=0;i<iarray.getlength();i++)
{
iarray[i]=i;
}
Array<double>darray(5);
for (i=0;i<darray.getlength();i++)
{
darray[i]=i*2.1;
}
return 0;
}