1 模板的概念

模板就是建立通用的模具,大大提高复用性

模板的特点:

  • 模板不可以直接使用,它只是一个框架
  • 模板的通用并不是万能的

2 函数模板

  • C++另一种编程思想称为 泛型编程 ,主要利用的技术就是模板
  • C++提供两种模板机制:函数模板类模板

2.1 函数模板语法

函数模板作用:
建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来代表。

语法:

  1. template<typename T>
  2. 函数声明或定义

解释:

  • template — 声明创建模板
  • typename — 表面其后面的符号是一种数据类型,可以用class代替
  • T — 通用的数据类型,名称可以替换,通常为大写字母

示例:

  1. #include<iostream>
  2. using namespace std;
  3. //交换整型函数
  4. void swapInt(int& a, int& b) {
  5. int temp = a;
  6. a = b;
  7. b = temp;
  8. }
  9. //交换浮点型函数
  10. void swapDouble(double& a, double& b) {
  11. double temp = a;
  12. a = b;
  13. b = temp;
  14. }
  15. //利用模板提供通用的交换函数
  16. template<typename T>
  17. void mySwap(T& a, T& b)
  18. {
  19. T temp = a;
  20. a = b;
  21. b = temp;
  22. }
  23. void test01()
  24. {
  25. int a = 10;
  26. int b = 20;
  27. //swapInt(a, b);
  28. //利用模板实现交换
  29. //1、自动类型推导
  30. mySwap(a, b);
  31. //2、显示指定类型
  32. mySwap<int>(a, b);
  33. cout << "a = " << a << endl;
  34. cout << "b = " << b << endl;
  35. }
  36. int main() {
  37. test01();
  38. return 0;
  39. }

运行结果:
image.png
总结:

  • 函数模板利用关键字 template
  • 使用函数模板有两种方式:自动类型推导、显示指定类型
  • 模板的目的是为了提高复用性,将类型参数化

2.2 函数模板注意事项

注意事项:

  • 自动类型推导,必须推导出一致的数据类型T,才可以使用
  • 模板必须要确定出T的数据类型,才可以使用

示例:

  1. #include<iostream>
  2. using namespace std;
  3. //利用模板提供通用的交换函数
  4. template<class T>
  5. void mySwap(T& a, T& b)
  6. {
  7. T temp = a;
  8. a = b;
  9. b = temp;
  10. }
  11. // 1、自动类型推导,必须推导出一致的数据类型T,才可以使用
  12. void test01()
  13. {
  14. int a = 10;
  15. int b = 20;
  16. char c = 'c';
  17. mySwap(a, b); // 正确,可以推导出一致的T
  18. //mySwap(a, c); // 错误,推导不出一致的T类型
  19. }
  20. // 2、模板必须要确定出T的数据类型,才可以使用
  21. template<class T>
  22. void func()
  23. {
  24. cout << "func 调用" << endl;
  25. }
  26. void test02()
  27. {
  28. //func(); //错误,模板不能独立使用,必须确定出T的类型
  29. func<int>(); //利用显示指定类型的方式,给T一个类型,才可以使用该模板
  30. }
  31. int main() {
  32. test01();
  33. test02();
  34. return 0;
  35. }

运行结果:
image.png
总结:

  • 使用模板时必须确定出通用数据类型T,并且能够推导出一致的类型

2.3 函数模板案例

案例描述:

  • 利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序
  • 排序规则从大到小,排序算法为选择排序
  • 分别利用char数组int数组进行测试

示例:

  1. #include<iostream>
  2. using namespace std;
  3. //交换的函数模板
  4. template<typename T>
  5. void mySwap(T &a, T&b)
  6. {
  7. T temp = a;
  8. a = b;
  9. b = temp;
  10. }
  11. template<class T> // 也可以替换成typename
  12. //利用选择排序,进行对数组从大到小的排序
  13. void mySort(T arr[], int len)
  14. {
  15. for (int i = 0; i < len; i++)
  16. {
  17. int max = i; //最大数的下标
  18. for (int j = i + 1; j < len; j++)
  19. {
  20. if (arr[max] < arr[j])
  21. {
  22. max = j;
  23. }
  24. }
  25. if (max != i) //如果最大数的下标不是i,交换两者
  26. {
  27. mySwap(arr[max], arr[i]);
  28. }
  29. }
  30. }
  31. template<typename T>
  32. void printArray(T arr[], int len) {
  33. for (int i = 0; i < len; i++) {
  34. cout << arr[i] << " ";
  35. }
  36. cout << endl;
  37. }
  38. void test01()
  39. {
  40. //测试char数组
  41. char charArr[] = "bdcfeagh";
  42. int num = sizeof(charArr) / sizeof(char);
  43. mySort(charArr, num);
  44. printArray(charArr, num);
  45. }
  46. void test02()
  47. {
  48. //测试int数组
  49. int intArr[] = { 7, 5, 8, 1, 3, 9, 2, 4, 6 };
  50. int num = sizeof(intArr) / sizeof(int);
  51. mySort(intArr, num);
  52. printArray(intArr, num);
  53. }
  54. int main() {
  55. test01();
  56. test02();
  57. return 0;
  58. }

运行结果:
image.png
总结:模板可以提高代码复用,需要熟练掌握

2.4 普通函数与函数模板的区别

普通函数与函数模板区别:

  • 普通函数调用时可以发生自动类型转换(隐式类型转换)
  • 函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换
  • 如果利用显示指定类型的方式,可以发生隐式类型转换

示例:

  1. #include<iostream>
  2. using namespace std;
  3. //普通函数
  4. int myAdd01(int a, int b)
  5. {
  6. return a + b;
  7. }
  8. //函数模板
  9. template<class T>
  10. T myAdd02(T a, T b)
  11. {
  12. return a + b;
  13. }
  14. //使用函数模板时,如果用自动类型推导,不会发生自动类型转换,即隐式类型转换
  15. void test01()
  16. {
  17. int a = 10;
  18. int b = 20;
  19. char c = 'c';
  20. cout << myAdd01(a, c) << endl; //正确,将char类型的'c'隐式转换为int类型 'c' 对应 ASCII码 99
  21. //myAdd02(a, c); // 报错,使用自动类型推导时,不会发生隐式类型转换
  22. myAdd02<int>(a, c); //正确,如果用显示指定类型,可以发生隐式类型转换
  23. }
  24. int main() {
  25. test01();
  26. return 0;
  27. }

运行结果:
image.png
总结:建议使用显示指定类型的方式,调用函数模板,因为可以自己确定通用类型T

2.5 普通函数与函数模板的调用规则

调用规则如下:

  1. 如果函数模板和普通函数都可以实现,优先调用普通函数
  2. 可以通过空模板参数列表来强制调用函数模板
  3. 函数模板也可以发生重载
  4. 如果函数模板可以产生更好的匹配,优先调用函数模板

示例:

  1. #include<iostream>
  2. using namespace std;
  3. //普通函数与函数模板调用规则
  4. void myPrint(int a, int b)
  5. {
  6. cout << "调用的普通函数" << endl;
  7. }
  8. template<typename T>
  9. void myPrint(T a, T b)
  10. {
  11. cout << "调用的模板" << endl;
  12. }
  13. template<typename T>
  14. void myPrint(T a, T b, T c)
  15. {
  16. cout << "调用重载的模板" << endl;
  17. }
  18. void test01()
  19. {
  20. //1、如果函数模板和普通函数都可以实现,优先调用普通函数
  21. // 注意 如果告诉编译器 普通函数是有的,但只是声明没有实现,或者不在当前文件内实现,就会报错找不到
  22. int a = 10;
  23. int b = 20;
  24. myPrint(a, b); //调用普通函数
  25. //2、可以通过空模板参数列表来强制调用函数模板
  26. myPrint<>(a, b); //调用函数模板
  27. //3、函数模板也可以发生重载
  28. int c = 30;
  29. myPrint(a, b, c); //调用重载的函数模板
  30. //4、 如果函数模板可以产生更好的匹配,优先调用函数模板
  31. char c1 = 'a';
  32. char c2 = 'b';
  33. myPrint(c1, c2); //调用函数模板
  34. }
  35. int main() {
  36. test01();
  37. return 0;
  38. }

运行结果:
image.png
总结:既然提供了函数模板,最好就不要提供普通函数,否则容易出现二义性

2.6 模板的局限性

局限性:模板的通用性并不是万能的

例1:下面的代码中提供的赋值操作,如果传入的a和b是一个数组,就无法实现了

  1. template<class T>
  2. void f(T a, T b)
  3. {
  4. a = b;
  5. }

例2:下面的代码中,如果T的数据类型传入的是像Person这样的自定义数据类型,也无法正常运行
因此C++为了解决这种问题,提供模板的重载,可以为这些特定的类型提供具体化的模板

  1. template<class T>
  2. void f(T a, T b)
  3. {
  4. if(a > b) { ... }
  5. }

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <string>
  4. class Person
  5. {
  6. public:
  7. Person(string name, int age)
  8. {
  9. this->m_Name = name;
  10. this->m_Age = age;
  11. }
  12. string m_Name;
  13. int m_Age;
  14. };
  15. //普通函数模板
  16. template<class T>
  17. bool myCompare(T& a, T& b)
  18. {
  19. if (a == b)
  20. {
  21. return true;
  22. }
  23. else
  24. {
  25. return false;
  26. }
  27. }
  28. //具体化,显示具体化的原型和定意思以template<>开头,并通过名称来指出类型
  29. //具体化优先于常规模板
  30. template<> bool myCompare(Person &p1, Person &p2)
  31. {
  32. if ( p1.m_Name == p2.m_Name && p1.m_Age == p2.m_Age)
  33. {
  34. return true;
  35. }
  36. else
  37. {
  38. return false;
  39. }
  40. }
  41. void test01()
  42. {
  43. int a = 10;
  44. int b = 20;
  45. //内置数据类型可以直接使用通用的函数模板
  46. bool ret = myCompare(a, b);
  47. if (ret)
  48. {
  49. cout << "a == b " << endl;
  50. }
  51. else
  52. {
  53. cout << "a != b " << endl;
  54. }
  55. }
  56. void test02()
  57. {
  58. Person p1("Tom", 10);
  59. Person p2("Tom", 10);
  60. //自定义数据类型,不会调用普通的函数模板
  61. //可以创建具体化的Person数据类型的模板,用于特殊处理这个类型
  62. bool ret = myCompare(p1, p2);
  63. if (ret)
  64. {
  65. cout << "p1 == p2 " << endl;
  66. }
  67. else
  68. {
  69. cout << "p1 != p2 " << endl;
  70. }
  71. }
  72. int main() {
  73. test01();
  74. test02();
  75. return 0;
  76. }

运行结果:
image.png
总结:

  • 利用具体化的模板,可以解决自定义类型的通用化
  • 学习模板并不是为了写模板,而是在STL能够运用系统提供的模板

3 类模板

3.1 类模板语法

类模板作用:建立一个通用类,类中的成员、数据类型可以不具体制定,用一个虚拟的类型来代表。

语法:

  1. template<typename T>

解释:

  • template — 声明创建模板
  • typename — 表面其后面的符号是一种数据类型,可以用class代替
  • T — 通用的数据类型,名称可以替换,通常为大写字母

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <string>
  4. //类模板
  5. template<class NameType, class AgeType>
  6. class Person
  7. {
  8. public:
  9. Person(NameType name, AgeType age)
  10. {
  11. this->mName = name;
  12. this->mAge = age;
  13. }
  14. void showPerson()
  15. {
  16. cout << "name: " << this->mName << " age: " << this->mAge << endl;
  17. }
  18. public:
  19. NameType mName;
  20. AgeType mAge;
  21. };
  22. void test01()
  23. {
  24. // 指定NameType 为string类型,AgeType 为 int类型
  25. Person<string, int>P1("孙悟空", 999);
  26. P1.showPerson();
  27. }
  28. int main() {
  29. test01();
  30. return 0;
  31. }

运行结果:
image.png
总结:类模板和函数模板语法相似,在声明模板template后面加类,此类称为类模板

3.2 类模板与函数模板区别

类模板与函数模板区别主要有两点:

  • 类模板没有自动类型推导的使用方式
  • 类模板在模板参数列表中可以有默认参数

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <string>
  4. //类模板
  5. template<class NameType, class AgeType = int>
  6. class Person
  7. {
  8. public:
  9. Person(NameType name, AgeType age)
  10. {
  11. this->mName = name;
  12. this->mAge = age;
  13. }
  14. void showPerson()
  15. {
  16. cout << "name: " << this->mName << " age: " << this->mAge << endl;
  17. }
  18. public:
  19. NameType mName;
  20. AgeType mAge;
  21. };
  22. //1、类模板没有自动类型推导的使用方式
  23. void test01()
  24. {
  25. // Person p("孙悟空", 1000); // 错误 类模板使用时候,不可以用自动类型推导
  26. Person <string ,int>p("孙悟空", 1000); //必须使用显示指定类型的方式,使用类模板
  27. p.showPerson();
  28. }
  29. //2、类模板在模板参数列表中可以有默认参数
  30. void test02()
  31. {
  32. Person <string> p("猪八戒", 999); //类模板中的模板参数列表 可以指定默认参数
  33. p.showPerson();
  34. }
  35. int main() {
  36. test01();
  37. test02();
  38. return 0;
  39. }

运行结果:
image.png
总结:

  • 类模板使用只能用显示指定类型方式
  • 类模板中的模板参数列表可以有默认参数

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

类模板中成员函数和普通类中成员函数创建时机是有区别的:

  • 普通类中的成员函数一开始就可以创建
  • 类模板中的成员函数在调用时才创建

示例:

  1. #include<iostream>
  2. using namespace std;
  3. class Person1
  4. {
  5. public:
  6. void showPerson1()
  7. {
  8. cout << "Person1 show" << endl;
  9. }
  10. };
  11. class Person2
  12. {
  13. public:
  14. void showPerson2()
  15. {
  16. cout << "Person2 show" << endl;
  17. }
  18. };
  19. template<class T>
  20. class MyClass
  21. {
  22. public:
  23. T obj;
  24. //类模板中的成员函数,并不是一开始就创建的,而是在模板调用时再生成
  25. void fun1() { obj.showPerson1(); }
  26. void fun2() { obj.showPerson2(); }
  27. };
  28. void test01()
  29. {
  30. MyClass<Person1> m;
  31. m.fun1();
  32. //m.fun2();//编译会出错,说明函数调用才会去创建成员函数
  33. }
  34. int main() {
  35. test01();
  36. return 0;
  37. }

运行结果:
image.png
总结:类模板中的成员函数并不是一开始就创建的,在调用时才去创建

3.4 类模板对象做函数参数

学习目标:类模板实例化出的对象,向函数传参的方式

一共有三种传入方式:

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

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <string>
  4. //类模板
  5. template<class NameType, class AgeType = int>
  6. class Person
  7. {
  8. public:
  9. Person(NameType name, AgeType age)
  10. {
  11. this->mName = name;
  12. this->mAge = age;
  13. }
  14. void showPerson()
  15. {
  16. cout << "name: " << this->mName << " age: " << this->mAge << endl;
  17. }
  18. public:
  19. NameType mName;
  20. AgeType mAge;
  21. };
  22. //1、指定传入的类型
  23. void printPerson1(Person<string, int> &p)
  24. {
  25. p.showPerson();
  26. }
  27. void test01()
  28. {
  29. Person <string, int >p("孙悟空", 100);
  30. printPerson1(p);
  31. }
  32. //2、参数模板化
  33. template <class T1, class T2>
  34. void printPerson2(Person<T1, T2>&p)
  35. {
  36. p.showPerson();
  37. cout << "T1的类型为: " << typeid(T1).name() << endl;
  38. cout << "T2的类型为: " << typeid(T2).name() << endl;
  39. }
  40. void test02()
  41. {
  42. Person <string, int >p("猪八戒", 90);
  43. printPerson2(p);
  44. }
  45. //3、整个类模板化
  46. template<class T>
  47. void printPerson3(T & p)
  48. {
  49. cout << "T的类型为: " << typeid(T).name() << endl;
  50. p.showPerson();
  51. }
  52. void test03()
  53. {
  54. Person <string, int >p("唐僧", 30);
  55. printPerson3(p);
  56. }
  57. int main() {
  58. test01();
  59. test02();
  60. test03();
  61. return 0;
  62. }

运行结果:
image.png
总结:

  • 通过类模板创建的对象,可以有三种方式向函数中进行传参
  • 使用比较广泛是第一种:指定传入的类型

3.5 类模板与继承

当类模板碰到继承时,需要注意一下几点:

  • 当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型
  • 如果不指定,编译器无法给子类分配内存
  • 如果想灵活指定出父类中T的类型,子类也需变为类模板

示例:

  1. #include<iostream>
  2. using namespace std;
  3. template<class T>
  4. class Base
  5. {
  6. T m;
  7. };
  8. //class Son:public Base //错误,c++编译需要给子类分配内存,必须知道父类中T的类型才可以向下继承
  9. class Son :public Base<int> //必须指定一个类型
  10. {
  11. };
  12. void test01()
  13. {
  14. Son c;
  15. }
  16. //类模板继承类模板 ,可以用T2指定父类中的T类型
  17. template<class T1, class T2>
  18. class Son2 :public Base<T2>
  19. {
  20. public:
  21. Son2()
  22. {
  23. cout << typeid(T1).name() << endl;
  24. cout << typeid(T2).name() << endl;
  25. }
  26. };
  27. void test02()
  28. {
  29. Son2<int, char> child1;
  30. }
  31. int main() {
  32. test01();
  33. test02();
  34. return 0;
  35. }

运行结果:
image.png
总结:如果父类是类模板,子类需要指定出父类中T的数据类型

3.6 类模板成员函数类外实现

学习目标:能够掌握类模板中的成员函数类外实现
示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <string>
  4. //类模板中成员函数类外实现
  5. template<class T1, class T2>
  6. class Person {
  7. public:
  8. //成员函数类内声明
  9. Person(T1 name, T2 age);
  10. void showPerson();
  11. public:
  12. T1 m_Name;
  13. T2 m_Age;
  14. };
  15. //构造函数 类外实现
  16. template<class T1, class T2>
  17. Person<T1, T2>::Person(T1 name, T2 age) {
  18. this->m_Name = name;
  19. this->m_Age = age;
  20. }
  21. //成员函数 类外实现
  22. template<class T1, class T2>
  23. void Person<T1, T2>::showPerson() {
  24. cout << "姓名: " << this->m_Name << " 年龄:" << this->m_Age << endl;
  25. }
  26. void test01()
  27. {
  28. Person<string, int> p("Tom", 20);
  29. p.showPerson();
  30. }
  31. int main() {
  32. test01();
  33. return 0;
  34. }

运行结果:
image.png
总结:类模板中成员函数类外实现时,需要加上模板参数列表

3.7 类模板分文件编写

学习目标:

  • 掌握类模板成员函数分文件编写产生的问题以及解决方式

问题:

  • 类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到

解决:

  • 解决方式1:直接包含.cpp源文件
  • 解决方式2:将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制

示例:
person.hpp中代码:

  1. #pragma once
  2. #include <iostream>
  3. using namespace std;
  4. #include <string>
  5. template<class T1, class T2>
  6. class Person {
  7. public:
  8. Person(T1 name, T2 age);
  9. void showPerson();
  10. public:
  11. T1 m_Name;
  12. T2 m_Age;
  13. };
  14. //构造函数 类外实现
  15. template<class T1, class T2>
  16. Person<T1, T2>::Person(T1 name, T2 age) {
  17. this->m_Name = name;
  18. this->m_Age = age;
  19. }
  20. //成员函数 类外实现
  21. template<class T1, class T2>
  22. void Person<T1, T2>::showPerson() {
  23. cout << "姓名: " << this->m_Name << " 年龄:" << this->m_Age << endl;
  24. }

类模板分文件编写.cpp中代码

#include<iostream>
using namespace std;

//#include "person.h"
#include "person.cpp" //解决方式1,包含cpp源文件

//解决方式2,将声明和实现写到一起,文件后缀名改为.hpp
#include "person.hpp"
void test01()
{
    Person<string, int> p("Tom", 10);
    p.showPerson();
}

int main() {

    test01();

    return 0;
}

运行结果:
image.png
总结:主流的解决方式是第二种,将类模板成员函数写到一起,并将后缀名改为.hpp

3.8 类模板与友元

学习目标:掌握类模板配合友元函数的类内和类外实现

全局函数类内实现 - 直接在类内声明友元即可
全局函数类外实现 - 需要提前让编译器知道全局函数的存在
示例:

#include<iostream>
using namespace std;
#include <string>

//2、全局函数配合友元  类外实现 - 先做函数模板声明,下方在做函数模板定义,在做友元
template<class T1, class T2> class Person;

//如果声明了函数模板,可以将实现写到后面,否则需要将实现体写到类的前面让编译器提前看到
//template<class T1, class T2> void printPerson2(Person<T1, T2> & p); 

template<class T1, class T2>
void printPerson2(Person<T1, T2> & p)
{
    cout << "类外实现 ---- 姓名: " << p.m_Name << " 年龄:" << p.m_Age << endl;
}

template<class T1, class T2>
class Person
{
    //1、全局函数配合友元   类内实现
    friend void printPerson(Person<T1, T2> & p)
    {
        cout << "姓名: " << p.m_Name << " 年龄:" << p.m_Age << endl;
    }


    //全局函数配合友元  类外实现
    friend void printPerson2<>(Person<T1, T2> & p);

public:

    Person(T1 name, T2 age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }

private:
    T1 m_Name;
    T2 m_Age;

};

//1、全局函数在类内实现
void test01()
{
    Person <string, int >p("Tom", 20);
    printPerson(p);
}

//2、全局函数在类外实现
void test02()
{
    Person <string, int >p("Jerry", 30);
    printPerson2(p);
}

int main() {

    //test01();

    test02();

    return 0;
}

运行结果:
image.png
总结:建议全局函数做类内实现,用法简单,而且编译器可以直接识别

3.9 类模板案例

案例描述: 实现一个通用的数组类,要求如下:

  • 可以对内置数据类型以及自定义数据类型的数据进行存储
  • 将数组中的数据存储到堆区
  • 构造函数中可以传入数组的容量
  • 提供对应的拷贝构造函数以及operator=防止浅拷贝问题
  • 提供尾插法和尾删法对数组中的数据进行增加和删除
  • 可以通过下标的方式访问数组中的元素
  • 可以获取数组中当前元素个数和数组的容量

示例:
myArray.hpp中代码:

#pragma once
#include <iostream>
using namespace std;

template<class T>
class MyArray
{
public:

    //构造函数
    MyArray(int capacity)
    {
        this->m_Capacity = capacity;
        this->m_Size = 0;
        pAddress = new T[this->m_Capacity];
    }

    //拷贝构造
    MyArray(const MyArray & arr)
    {
        this->m_Capacity = arr.m_Capacity;
        this->m_Size = arr.m_Size;
        this->pAddress = new T[this->m_Capacity];
        for (int i = 0; i < this->m_Size; i++)
        {
            //如果T为对象,而且还包含指针,必须需要重载 = 操作符,因为这个等号不是 构造 而是赋值,
            // 普通类型可以直接= 但是指针类型需要深拷贝
            this->pAddress[i] = arr.pAddress[i];
        }
    }

    //重载= 操作符  防止浅拷贝问题
    MyArray& operator=(const MyArray& myarray) {

        if (this->pAddress != NULL) {
            delete[] this->pAddress;
            this->m_Capacity = 0;
            this->m_Size = 0;
        }

        this->m_Capacity = myarray.m_Capacity;
        this->m_Size = myarray.m_Size;
        this->pAddress = new T[this->m_Capacity];
        for (int i = 0; i < this->m_Size; i++) {
            this->pAddress[i] = myarray[i];
        }
        return *this;
    }

    //重载[] 操作符  arr[0]
    T& operator [](int index)
    {
        return this->pAddress[index]; //不考虑越界,用户自己去处理
    }

    //尾插法
    void Push_back(const T & val)
    {
        if (this->m_Capacity == this->m_Size)
        {
            return;
        }
        this->pAddress[this->m_Size] = val;
        this->m_Size++;
    }

    //尾删法
    void Pop_back()
    {
        if (this->m_Size == 0)
        {
            return;
        }
        this->m_Size--;
    }

    //获取数组容量
    int getCapacity()
    {
        return this->m_Capacity;
    }

    //获取数组大小
    int    getSize()
    {
        return this->m_Size;
    }


    //析构
    ~MyArray()
    {
        if (this->pAddress != NULL)
        {
            delete[] this->pAddress;
            this->pAddress = NULL;
            this->m_Capacity = 0;
            this->m_Size = 0;
        }
    }

private:
    T * pAddress;  //指向一个堆空间,这个空间存储真正的数据
    int m_Capacity; //容量
    int m_Size;   // 大小
};

类模板案例—数组类封装.cpp中

#include <iostream>
#include "myArray.hpp"
#include <string>
using namespace std;

void printIntArray(MyArray<int>& arr) {
    for (int i = 0; i < arr.getSize(); i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

//测试内置数据类型
void test01()
{
    MyArray<int> array1(10);
    for (int i = 0; i < 10; i++)
    {
        array1.Push_back(i);
    }
    cout << "array1打印输出:" << endl;
    printIntArray(array1);
    cout << "array1的大小:" << array1.getSize() << endl;
    cout << "array1的容量:" << array1.getCapacity() << endl;

    cout << "--------------------------" << endl;

    MyArray<int> array2(array1);
    array2.Pop_back();
    cout << "array2打印输出:" << endl;
    printIntArray(array2);
    cout << "array2的大小:" << array2.getSize() << endl;
    cout << "array2的容量:" << array2.getCapacity() << endl;
}

//测试自定义数据类型
class Person {
public:
    Person() {} 
        Person(string name, int age) {
        this->m_Name = name;
        this->m_Age = age;
    }
public:
    string m_Name;
    int m_Age;
};

void printPersonArray(MyArray<Person>& personArr)
{
    for (int i = 0; i < personArr.getSize(); i++) {
        cout << "姓名:" << personArr[i].m_Name << " 年龄: " << personArr[i].m_Age << endl;
    }

}

void test02()
{
    //创建数组
    MyArray<Person> pArray(10);
    Person p1("孙悟空", 30);
    Person p2("韩信", 20);
    Person p3("妲己", 18);
    Person p4("王昭君", 15);
    Person p5("赵云", 24);

    //插入数据
    pArray.Push_back(p1);
    pArray.Push_back(p2);
    pArray.Push_back(p3);
    pArray.Push_back(p4);
    pArray.Push_back(p5);

    printPersonArray(pArray);

    cout << "pArray的大小:" << pArray.getSize() << endl;
    cout << "pArray的容量:" << pArray.getCapacity() << endl;

}

int main() {

    //test01();

    test02();

    return 0;
}

运行结果:
image.png
总结:能够利用所学知识点实现通用的数组