65,210
社区成员
发帖
与我相关
我的任务
分享#include <iostream>
template<class T>
struct st { //这个 typedef 跟 C 学的吧……
T a;
T b;
st(T _a, T _b) : a(_a), b(_b) {} //如果想直接用两个量初始化,必须定义构造函数
};
template <class T> //这里也是个模板函数
T maxx(st<T> x)
{
if(x.a>x.b)
return x.a;
else
return x.b;
}
int main()
{
using std::cout;
using std::endl;
cout<<maxx(st<double>(4.6,7.8))<<endl; //类的构造函数不支持直接使用模板参数解析,只能自己指定模板参数
cout<<maxx(st<int>(5,8))<<endl;
}