1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. //类模板对象做函数的参数
    5. //1、指定传入类型
    6. //2、参数模板化
    7. //3、整个类模板化
    8. template<class T1, class T2>
    9. class person {
    10. public:
    11. person(T1 name, T2 age) {
    12. m_name = name;
    13. m_age = age;
    14. }
    15. void show() {
    16. cout << "姓名:" << m_name << " 年龄:" << m_age << endl;
    17. }
    18. T1 m_name;
    19. T2 m_age;
    20. };
    21. //1、指定传入类型
    22. void print1(person<string, int>&p) {
    23. p.show();
    24. }
    25. void test01() {
    26. person<string, int>p("孙悟空",999);
    27. print1(p);
    28. }
    29. //2、参数模板化
    30. template<class T1, class T2>
    31. void print2(person<T1, T2>&p) {
    32. p.show();
    33. }
    34. void test02() {
    35. person<string, int>p("猪八戒", 888);
    36. print2(p);
    37. }
    38. //3、整个类模板化
    39. template<class T>
    40. void print3(T & p) {
    41. p.show();
    42. }
    43. void test03() {
    44. person<string, int>p("沙师弟", 777);
    45. print3(p);
    46. }
    47. int main() {
    48. test01();
    49. test02();
    50. test03();
    51. return 0;
    52. }

    类模板实例化出的对象向函数传参的方式:
    1、指定传入类型
    2、参数模板化
    3、整个类模板化