关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

C++函数模板与类模板

发布时间:2023-06-30 16:00:54

C++另一种编程思想被称为泛型编程,主要利用的技术就是模板

C++提供两种模板机制:函数模板类模板

模板的特点:

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

函数模板语法

函数模板作用:

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

语法:template

  • template:声明创建模板
  • typename:表明其后面的符号是一种数据类型,可以用class代替
  • T:通用的数据类型,名称可以替换,通常为大写字母
#includeusing namespace std; //函数模板 template//告诉编译器,后面的代码中紧跟着的T不要报错,T是一个通用数据类型 void mySwap(T& a, T& b) {  T temp = a;  a = b;  b = temp; } int main() {  int a = 10;  int b = 20;  //两种方法使用函数模板  //自动类型推导  mySwap(a, b);  cout << "a=" << a << "\tb=" << b << endl;  //显式指定类型  mySwap(a, b);  cout << "a=" << a << "\tb=" << b << endl;  return 0; }

   

函数模板注意事项

  • 自动类型推导,必须推导出一致的数据类型T才可以使用
  • 模板必须要确定出T的数据类型,才可以使用
//模板必须要确定出T的数据类型,才可以使用 template//注释掉可正常运行 void func(T& a, T& b) {  cout << "hello" << endl; } int main() {  func();//没有与参数列表匹配的函数模板实例  return 0; }

   

普通函数与函数模板区别

  • 普通函数调用时可以发生自动类型转换(隐式类型转换)
  • 函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换
  • 如果利用显示指定类型的方式,可以发生隐式类型转换
    • 建议使用显示指定类型的方式调用函数模板,因为自己可以确定通用类型T
templateT func(T a, T b) {  return a + b; } int main() {  int a = 10;  char b = 10;  //如果利用显示指定类型的方式,可以发生隐式类型转换  cout << func(a, b) << endl;//可正常运行  //如果利用自动类型推导,不会发生隐式类型转换  cout << func(a, b) << endl;//会报错  return 0; }

   

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

  • 如果函数模板和普通函数都可以实现,优先调用普通函数
  • 可以通过空模板参数列表来强制调用函数模板
  • 函数模板也可以发生重载
  • 如果函数模板可以更好地匹配,优先调用函数模板
  • 既然提供了函数模板,最好就不要提供普通函数,否则容易出现二义性
void func(int a, int b) {  cout << "普通函数" << endl; } templatevoid func(T a, T b) {  cout << "模板" << endl; } int main() {  //如果函数模板和普通函数都可以实现,优先调用普通函数  func(1, 1);  //通过空模板参数列表来强制调用函数模板  func<>(1, 1);  return 0; }

     

templatevoid func(T a, T b) {  cout << "模板" << endl; } templatevoid func(T a, T b,T c) {  cout << "重载的模板" << endl; } int main() {  func<>(1, 1);  //函数模板也可以发生重载  func<>(1, 1, 1);  return 0; }

   

模板的局限性

模板不是万能的,有些特定数据类型,需要用具体化方式做特殊实现

  • 利用具体化的模板,可以解决自定义类型的通用化
  • 学习模板并不是为了写模板,而是在STL能够运用系统提供的模板
class Person { public:  Person(string name, int age)  {  m_name = name;  m_age = age;  }  string m_name;  int m_age; }; templatebool func(T& a, T& b) {  if (a == b)  return true;  else  return false; } //利用具体化Person的版本实现代码,具体化优先调用 template<> bool func(Person& a, Person& b) {  if (a.m_name == b.m_name && a.m_age == b.m_age)  return true;  else  return false; }

   

类模板语法

类模板作用:

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

语法:template

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

类模板和函数模板语法相似,在声明模板template后面加类,此类称为类模板

templateclass Person { public:  Person(NameType name, AgeType age)  {  this->m_name = name;  this->m_age = age;  }  void show()  {  cout << m_name << m_age << endl;  }  NameType m_name;  AgeType m_age; }; int main() {  Persona("张三", 10);  a.show();  return 0; }

   

类模板与函数模板区别

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

  • 类模板没有自动类型推导的使用方式
  • 类模板在模板参数列表中可以有默认参数
//类模板中可以有默认参数 templateclass Person { public:  Person(NameType name, AgeType age)  {  this->m_name = name;  this->m_age = age;  }  void show()  {  cout << m_name << m_age << endl;  }  NameType m_name;  AgeType m_age; }; int main() {  //类模板没有自动类型推导的使用方式  //Person a("张三", 10);  Persona("张三", 10);//只能用显示指定类型  a.show();  return 0; }

   

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

  • 普通类中的成员函数一开始就可以创建
  • 类模板中的成员函数在调用时才创建
class A { public:  void show()  {  cout << "A" << endl;  } }; templateclass B { public:  T t;  void show()  {  t.show();  } }; int main() {  B b;  b.show();  return 0; }

   

类模板对象做函数参数

类模板实例化出的对象,有三种向函数传参的方式:

  • 指定传入的类型:直接显示对象的数据类型
  • 参数模板化:将对象中的参数变为模板进行传递
  • 整个类模板化:将这个对象类型模板化进行传递
  • 使用比较广泛的是第一种:指定传入类型
templateclass A { public:  A(T t)  {  this->m_t = t;  }  T m_t;  void show()  {  cout << m_t << endl;  } }; //指定传入类型 void show(A& a) {  a.show(); } int main() {  Aa(10);  show(a);  return 0; }

     

templateclass A { public:  A(T t)  {  this->m_t = t;  }  T m_t;  void show()  {  cout << m_t << endl;  } }; //参数模板化 templatevoid show(A& a) {  a.show(); } int main() {  Aa(10);  show(a);  return 0; }

     

templateclass A { public:  A(T t)  {  this->m_t = t;  }  T m_t;  void show()  {  cout << m_t << endl;  } }; //参数模板化 templatevoid show(T a) {  a.show();  cout << "T的类型为" << typeid(T).name() << endl; } int main() {  Aa(10);  show(a);  return 0; }

   

类模板与继承

  • 当子类继承的父类是一个类模板时,子类在声明时,要指定出父类中T的类型
  • 如果不指定,编译器无法给子类分配内存
  • 如果想灵活指定出父类中T的类型,子类也需变为类模板
templateclass A { }; //必须知道父类中T的类型,才能继承给子类 class B :public A{ };

     

class A { }; //如果想灵活指定父类中T的类型,子类也需要变为类模板 templateclass B :public A{ };

   

类模板成员函数类外实现

templateclass Person { public:  Person(T1 name, T2 age);  T1 m_name;  T2 m_age; }; //构造函数类外实现 templatePerson::Person(T1 name, T2 age) {  this->m_age = age;  this->m_name = name; }

     

templateclass Person { public:  void show();  T1 m_name;  T2 m_age; }; //成员函数类外实现 templatevoid Person::show() {  cout << m_name << "\t" << m_age << endl; }

   

类模板分文件编写

问题:

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

两种解决方式:

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

类模板与友元

全局函数类内实现:直接在类内声明友元即可

全局函数类外实现:需要提前让编译器知道全局函数的存在

建议全局函数做类内实现,用法简单,而且编译器可以直接识别

templateclass Test {   friend void func(Testtest)  {  cout << "全局函数类内实现 " << test.m_name << endl;  }  T1 m_name; public:  Test(T1 name)  {  this->m_name = name;  } }; int main() {  Testa("张三");  func(a);  return 0; }

     

templateclass Test; templatevoid func(Testtest); //用到的东西要提前让编译器知道 templateclass Test {  //加空模板参数列表,调用函数模板  friend void func<>(Testtest);  T1 m_name; public:  Test(T1 name)  {  this->m_name = name;  } }; templatevoid func(Testtest) {  cout << "全局函数类外实现 " << test.m_name << endl; } int main() {  Testa("张三");  func(a);  return 0; }

   

自制数组类

直接包含.hpp即可使用

#pragma once //自己的通用数组类 #include#includeusing namespace std; templateclass MyArray { public:  MyArray(int capacity)  {  cout << "有参构造" << endl;  this->m_Capacity = capacity;  this->m_Size = 0;  this->pAddress = new T[this->m_Capacity];  }  MyArray(const MyArray& arr)  {  cout << "拷贝构造" << endl;  this->m_Capacity = arr.m_Capacity;  this->m_Size = arr.m_Size;  //深拷贝  this->pAddress = new T[arr.m_Capacity];  //将arr中的数据都拷贝过来  for (int i = 0; i < this->m_Size; i++)  {  this->pAddress[i] = arr.pAddress[i];  }  }  ~MyArray()  {  if (this->pAddress != NULL)  {  cout << "析构" << endl;  delete[] this->pAddress;  this->pAddress = NULL;  }  }  MyArray& operator=(const MyArray& arr)  {  //先判断原来堆区是否有数据,如果有,先释放  if (pAddress != NULL)  {  cout << "operator=调用" << endl;  delete[] this->pAddress;  this->pAddress = NULL;  this->m_Capacity = 0;  this->m_Size = 0;  }  //深拷贝  this->m_Capacity = arr.m_Capacity;  this->m_Size = arr.m_Size;  this->pAddress = new T[arr.m_Capacity];  for (int i = 0; i < this->m_Size; i++)  {  this->pAddress[i] = arr.pAddress[i];  }  return *this;  }  //尾插法  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 (m_Size == 0)  {  return;  }  this->m_Size--;  }  //通过下标访问数组中的元素  T& operator[](int index)  {  return this->pAddress[index];//不考虑边界,由用户自行处理  }  //返回数组容量  int getCapacity()  {  return this->m_Capacity;  }  //返回数组大小  int getSize()  {  return this->m_Size;  } private:  T* pAddress;//指针指向堆区开辟的真实数组  int m_Capacity;  int m_Size; };

/template/Home/leiyu/PC/Static