简介
明白类型是什么即可,可以适当运用using语法起别名 简化代码。
一、函数模板嵌套
template <class _Ty1,class _Ty2>
class MM
{
public:
MM(_Ty1 one, _Ty2 two) :one(one), two(two) {}
friend ostream& operator<<(ostream& out, const MM& mm)
{
out << mm.one << " " << mm.two;
return out;
}
protected:
_Ty1 one;
_Ty2 two;
};
template <class _Ty>
void print(_Ty data)
{
cout << data << endl;
}
int main()
{
print(MM<string, int>("小芳", 32));
print<MM<string, int>>(MM<string, int>("小美", 28));
using MMType = MM<string, int>;
print<MMType>(MMType("小美", 28));
return 0;
}
小芳 32
小美 28
二、类模板嵌套
template <class _Ty1,class _Ty2>
class MM
{
public:
MM(_Ty1 one, _Ty2 two) :one(one), two(two) {}
friend ostream& operator<<(ostream& out, const MM& mm)
{
out << mm.one << " " << mm.two;
return out;
}
protected:
_Ty1 one;
_Ty2 two;
};
template <class _Ty1,class _Ty2>
class Data
{
public:
Data(_Ty1 one, _Ty2 two) :one(one), two(two) {}
void print()
{
cout << one <<" "<< two << endl;
}
protected:
_Ty1 one;
_Ty2 two;
};
void testFunc()
{
Data<MM<string, int>, MM<double, double>>
data(MM<string, int>("小芳",18), MM<double, double>(89,56));
data.print();
MM<string, int> mmData("小芳", 18);
MM<double, double> mmScore(89, 56);
Data<MM<string, int>, MM<double, double>> mData(mmData, mmScore);
mData.print();
}
int main()
{
testFunc();
}
小芳 18 89 56
小芳 18 89 56
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)