walker摘自这篇文章:C++学习:模板类学习
源码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#include <iostream>
using
namespace
std;
//函数模板
template
<
typename
T>
T maxx(
const
T &v1,
const
T &v2)
{
return
(v1 > v2) ? v1 : v2;
}
//类模板
template
<
typename
T>
class
Compare
{
private
:
T a;
T b;
public
:
Compare(T a, T b)
{
this
->a = a;
this
->b = b;
}
T max()
const
{
return
a > b ? a : b;
}
};
int
main()
{
cout <<
"max(3, 5): "
<< maxx(3, 5) << endl;
Compare<
int
> cmp(3, 5);
cout <<
"cmp.max: "
<< cmp.max() << endl;
return
0;
}
|
运行截图:
*** walker ***
本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1332500如需转载请自行联系原作者
RQSLT