#include<iostream>
using namespace std;
template<class T, int MAXSIZE>
class Array
{
public:
Array()
{
head = new T[MAXSIZE];
cout << "MAXSIZE:" << MAXSIZE << endl;
}
~Array()
{
delete[] head;
}
private:
T *head;
};
void array_init(int *array, int len)
{
for (int i = 0; i < len; i++)
{
array[i] = 0;
}
}
template<class T>
void array_init(T *array, int len)
{
for (int i = 0; i < len; i++)
{
array[i] = 0;
}
}
template<class T, int len>
void array_init(T(&array)[len])//surprise!
{
cout << "len:" << len << endl;
for (int i = 0; i < len; i++)
{
array[i] = 0;
}
}
void main()
{
Array<int, 100> arr;
int array[100];
array_init(array, 100);
array_init(array);
}
泛型与非泛型组合
最新推荐文章于 2024-08-25 16:40:47 发布