#include "stdafx.h"
#include "iostream"
#include <ctime>
using namespace std;
//全局常量size=4
const int size=4;
template <typename T>
class MyClass
{
public:
MyClass(T* p)
{
for (int i = 0; i < size;i++)
{
array[i] = p[i];
}
}
void print();
private:
T array[size];
};
template <typename T>
//模板类函数定义需要加MyClass<T>
void MyClass<T>::print()
{
for (int i = 0; i < size;i++)
{
cout << array[i] << ' ';
}
}
int _tmain()
{
int intArray[size] = { 1, 2, 3, 4 };
//MyClass <double> obj(intArray);
//double需要改成int,类的构造函数的参数要和模板类参数一致
MyClass <int> obj(intArray);
obj.print();
cout << endl;
return 0;
}