forked from muziing/CPP_Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_类模板对象做函数参数.cpp
78 lines (66 loc) · 1.35 KB
/
10_类模板对象做函数参数.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
using namespace std;
// 类模板对象做函数参数
// 1.指定传入的类型 -- 直接显示对象的数据类型
// 2.参数模板化 -- 将对象中的参数变为模板进行传递
// 3.整个类模板化 -- 将这个对象类型模板化进行传递
template <class T1, class T2>
class Person
{
public:
Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << "name: " << this->m_Name
<< "\tage: " << this->m_Age
<< endl;
}
T1 m_Name;
T2 m_Age;
};
// 1.指定传入的类型
void printPerson1(Person<string, int> &p)
{
p.showPerson();
}
void test01()
{
Person<string, int> p("Tom", 20);
printPerson1(p);
}
// 2.参数模板化
template <class T1, class T2>
void printPerson2(Person<T1, T2> &p)
{
p.showPerson();
cout << "T1 的数据类型为: " << typeid(T1).name() << endl;
cout << "T2 的数据类型为: " << typeid(T2).name() << endl;
}
void test02()
{
Person<string, int> p("Jerry", 19);
printPerson2(p);
}
// 3.整个类模板化
template <class T>
void printPerson3(T &p)
{
p.showPerson();
cout << typeid(T).name() << endl;
}
void test03()
{
Person<string, int> p("Max", 21);
printPerson3(p);
}
int main()
{
test01();
test02();
test03();
return 0;
}