/*
* 类模板的成员函数是函数模板
*/
#include <iostream>
#include <string>
using namespace std;
/******************************************************************************
/* 类模板 - 声明
/******************************************************************************/
template <class T> class A
{
public:
A();
A(T _a, T _b);
T sum();
private:
T a;
T b;
};
template <class T1, class T2>
struct tSample
{
T1 key;
T2 value;
};
/******************************************************************************
/* 类模板 - 实现
/******************************************************************************/
template <class T> A<T>::A() //类模板的成员函数是函数模板
{
a = 0; b = 0;
}
template <class T> A<T>::A(T _a, T _b) //类模板的成员函数是函数模板
{
a = _a; b = _b;
}
template <class T> T A<T>::sum() //类模板的成员函数是函数模板
{
return a + b;
}
void main()
{
A<int> obj1(3, 4);
A<double> obj2(2.33, 3.67);
cout<<"obj1.sum(3, 4)="<<obj1.sum()<<"\n"<<
"obj2.sum(2.33, 3.67)="<<obj2.sum()<<endl;
tSample<unsigned char, int> array1[] =
{
{255, 0xffffffff},
{256, 0xfffffff},
};
tSample<unsigned short, int> array2[] =
{
{255, 0xffffffff},
{256, 0xfffffff},
};
for (int idx = 0; idx < sizeof(array1) / sizeof(array1[0]); idx++)
{
printf("template struct array1, index: %d, key: %d, value: 0x%x \n", idx, \
array1[idx].key, array1[idx].value);
}
for (idx = 0; idx < sizeof(array2) / sizeof(array2[0]); idx++)
{
printf("template struct array2, index: %d, key: %d, value: 0x%x \n", idx, \
array2[idx].key, array2[idx].value);
}
}
* 类模板的成员函数是函数模板
*/
#include <iostream>
#include <string>
using namespace std;
/******************************************************************************
/* 类模板 - 声明
/******************************************************************************/
template <class T> class A
{
public:
A();
A(T _a, T _b);
T sum();
private:
T a;
T b;
};
template <class T1, class T2>
struct tSample
{
T1 key;
T2 value;
};
/******************************************************************************
/* 类模板 - 实现
/******************************************************************************/
template <class T> A<T>::A() //类模板的成员函数是函数模板
{
a = 0; b = 0;
}
template <class T> A<T>::A(T _a, T _b) //类模板的成员函数是函数模板
{
a = _a; b = _b;
}
template <class T> T A<T>::sum() //类模板的成员函数是函数模板
{
return a + b;
}
void main()
{
A<int> obj1(3, 4);
A<double> obj2(2.33, 3.67);
cout<<"obj1.sum(3, 4)="<<obj1.sum()<<"\n"<<
"obj2.sum(2.33, 3.67)="<<obj2.sum()<<endl;
tSample<unsigned char, int> array1[] =
{
{255, 0xffffffff},
{256, 0xfffffff},
};
tSample<unsigned short, int> array2[] =
{
{255, 0xffffffff},
{256, 0xfffffff},
};
for (int idx = 0; idx < sizeof(array1) / sizeof(array1[0]); idx++)
{
printf("template struct array1, index: %d, key: %d, value: 0x%x \n", idx, \
array1[idx].key, array1[idx].value);
}
for (idx = 0; idx < sizeof(array2) / sizeof(array2[0]); idx++)
{
printf("template struct array2, index: %d, key: %d, value: 0x%x \n", idx, \
array2[idx].key, array2[idx].value);
}
}