鱼C论坛

 找回密码
 立即注册
查看: 1202|回复: 0

[技术交流] C++旅程第10站——类模板2

[复制链接]
发表于 2020-6-3 22:16:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 这是她 于 2020-6-4 20:20 编辑

Concern for someone else was a good remedy for taking the mind off one's own troubles.


类模板中成员函数创建时机


类模板中成员变量和普通类中成员函数创建时机区别:
1、普通类的成员函数一开始就可以创建
2、类模板的成员函数在调用时才创建

  1. #include<iostream>

  2. using namespace std;

  3. class Student1
  4. {
  5.         public:
  6.                 void Showstudent1()
  7.                 {
  8.                         cout << "Student1!!!!!!!!!!!!!!!!!" << endl;
  9.                 }
  10. };

  11. class Student2
  12. {
  13.         public:
  14.                 void Showstudent2()
  15.                 {
  16.                         cout << "Student2!!!!!!!!!!!!!!!!!" << endl;
  17.                 }
  18. };

  19. template<class T>
  20. class Class
  21. {
  22.         public:
  23.         
  24.                 T a;
  25.                
  26.                 //类模板中的成员函数并不是一开始就创建的,而是在模板调用的时在生成
  27.                
  28.                 void func1()
  29.                 {
  30.                         a.Showstudent1();
  31.                 }        
  32.                
  33.                 void func2()
  34.                 {
  35.                         a.Showstudent2();
  36.                 }
  37. };

  38. void test1()
  39. {
  40.         //模板中的成员函数,比如fun1(),fun2(),只有在程序运行的时候才会被创建。
  41.         
  42.         Class<Student1> p1;
  43.         p1.func1();
  44.         p1.func2();//编译报错 --Student1类中没有func2()函数
  45.         
  46.         Class<Student2> p2;
  47.         p2.func1();//编译报错 --Student2类中没有func1()函数
  48.         p2.func2();
  49. }

  50. int main()
  51. {
  52.         test1();
  53.         
  54.         return 0;
  55. }
复制代码

类模板对象做函数参数


传入方式:
1、指定传入的类型              ——直接显示对象的数据类型
2、参数模板化                    ——将对象中的参数变成模板进行传递
3、整个类模板化                 ——将这个对象类型模板化进行传递

  1. #include<iostream>
  2. #include<string>

  3. using namespace std;

  4. //定义一个模板类
  5. template<class T1,class T2>
  6. class Person
  7. {
  8.         public:
  9.                 T1 m_name;
  10.                 T2 m_age;
  11.                
  12.                 Person(T1 name,T2 age)
  13.                 {
  14.                         this->m_name = name;
  15.                         this->m_age = age;
  16.                 }
  17.                
  18.                 void showperson()
  19.                 {
  20.                         cout << "name : " << this->m_name << "  age : " << this->m_age << endl;
  21.                 }
  22. };

  23. //1、指定传入类型 (常用)
  24. void Aperson(Person<string,int>&p)//传入引用
  25. {
  26.         p.showperson();
  27. }

  28. void test1()
  29. {
  30.         Person<string,int>p("猪头肉",100);
  31.         Aperson(p);
  32. }

  33. //2、参数模板化
  34. template<class T1,class T2>
  35. void Bperson(Person<T1,T2>&p)
  36. {
  37.         p.showperson();
  38. }

  39. void test2()
  40. {
  41.         Person<string,int>p("螺狮粉",90);
  42.         Bperson(p);
  43. }

  44. //3、整个类模板化
  45. template<class T>
  46. void Cperson(T &p)
  47. {
  48.         p.showperson();
  49. }

  50. void test3()
  51. {
  52.         Person<string,int>p("烤猪蹄",80);
  53.         Cperson(p);
  54. }

  55. int main()
  56. {
  57.         test1();
  58.         
  59.         test2();
  60.         
  61.         test3();
  62.         
  63.         return 0;
  64. }
复制代码

类模板与继承



当类模板碰到继承时,注意一下几点:
1、当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类的中T的类型
2、如果不指定,编译器无法给予子类分配内存
3、如果想灵活指定出父类中T的类型,子类也需变为类模板

  1. #include<iostream>

  2. using namespace std;

  3. template<class T>
  4. class Person
  5. {
  6.         public:
  7.                 T m;        
  8. };

  9. //class Person1 : public Person //错误  C++编译需要给子类分配内存,必须直到父类中T的类型才可以向下继承
  10. class Person1 : public Person<int>//必须指定一个类型
  11. {
  12.         
  13. };

  14. void test1()
  15. {
  16.         Person1 p1;
  17. }

  18. //如果想灵活指定父类中T类型,子类也需要变类模板
  19. template<class T1,class T2>
  20. class Person2 : public Person<T2>
  21. {
  22.         public:
  23.                 T1 s;
  24. };

  25. void test2()
  26. {
  27.         Person2<int,char>p2;//int->T1  char->T2 父类中T
  28. }

  29. int main()
  30. {
  31.         test1();
  32.         
  33.         test2();
  34.         
  35.         return 0;
  36. }
复制代码

                                                                  让我看看是谁再看

评分

参与人数 1荣誉 +1 鱼币 +5 贡献 +5 收起 理由
永恒的蓝色梦想 + 1 + 5 + 5 无条件支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-17 13:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表