- #include <iostream>
- struct box
- {
- int a;
- int b;
- int c;
- int d;
- };
- template <class T> //函数模板
- T MaxValue (T x,T y)
- {
- return x > y ? x:y;
- }
- template <> box MaxValue (box a,box b) //函数模板具体化
- {
- return a.a >b.a ? a : b;
- }
- using namespace std;
- void main()
- {
- cout <<MaxValue(555,666)<<endl;
- cout <<MaxValue(1.1,0.5)<<endl;
- box a={1,2,3,4};
- box b={2,3,4,5};
- cout <<MaxValue(a,b).a<<endl; //重载函数模板具体化
- }
C++ Primer Plus 第五版 复习题7 函数模板 函数模板具体化
最新推荐文章于 2025-07-20 12:45:35 发布