任务:模板和异常处理
模版:实现代码重用的一种工具,实现类型参数化
包括函数模板,类模板
template<typename T>//typename可换成class
函数类型 函数名(形参列表)
{
函数体
}//函数模板定义
上面的代码是接受二个相同数据类型
若要接受不同数据类型需要把
template<class T>变成template<class T,class U>
T GetMax(T a,T b)变成T GetMax(T a,U b)
类模板:
template<typename T>
class 类名{
成员列表
};
template<typename T>
函数类型 类名<T>::成员函数名(形参列表)
{
函数体
}//类外定义
类模板中的成员的参数或返回值可以是任意的数据类型
类模板把他实例化为具体的类