C++ 泛型编程详解分享!

泛型编程与面向对象编程的目标相同,即使重用代码和抽象通用概念的技术更加简单。但是面向对象编程强调编程的数据方面,泛型编程强调的是独立于特定数据类型。

这一篇介绍一下 C++ 编程中与面向对象并列的另一大分支——泛型编程,这一篇主要介绍函数模板、类模板和成员模板三大部分

如有侵权,请联系删除,如有错误,欢迎大家指正,谢谢

泛型编程

模板是泛型编程的一种重要思想,STL(Standard Template Library,标准模板库)是采用模板实现的一个实例
函数模板

对比函数重载(同一作用域内函数名相同,参数列表不同的函数),函数模板只需要一个函数就实现了函数重载的部分功能(参数个数相同类型不同,函数重载需要定义多个同名参数列表不同的函数)

  template<typename T, typename Y> // 这也可以写 template<class T, class Y> 此处的 class 和 typename 作用相同  void tfunc(T& t, Y& y) {   cout << t << " " << y << endl;  }  int n = 2;  double d = 2.1;  tfunc(n, d);    // 运行结果:2 2.1

函数模板具体化,函数模板具体化就是将某一(某几)个要处理的类型单独处理,需要单独写一个实现,形式是 template<> void fun(type& t);,函数模板的具体化和普通函数可以同时存在,调用顺序是 普通函数 > 函数模板具体化 > 模板函数

  // ====== 测试一:函数模板针对特殊数据类型具体化 ======  struct Node {   int val;   Node* next;  };  // 函数模板  template<typename T>   void tfunc(const T& t) {   cout << "template: " << t << endl;  }  // 函数模板具体化(用于处理Node类型数据)  template<>   void tfunc<Node>(const Node& node) {   cout << "template<Node>: " << node.val << endl;  }  // 函数模板具体化(用于处理int类型数据)  template<>   void tfunc<int>(const int& n) {   cout << "template<int>: " << n << endl;  }  // 普通函数  void tfunc(const int& n) {   cout << "tfunc(): " << n << endl;  }  double d = 2.1;  tfunc(d); // 函数模板未具体化double类型函数,调用模板  Node node{ 2, nullptr };  tfunc(node); // 函数模板具体化Node类型函数,调用函数模板的具体化  int n = 2;  tfunc(n); // 函数模板具体化int类型函数,也存在普通函数,调用普通函数  // ====== 测试二:函数模板部分具体化 ======  template<typename T1, typename T2>  void tfunc(T1 t1, T2 t2) {   cout << typeid(T1).name() << " and " << typeid(T2).name() <<": " << t1 << " " << t2 << endl;  }  template<typename T1>  void tfunc(T1 t1, int i) {   cout << typeid(T1).name() << " and " << "int: " << t1 << " " << i << endl;  }  template<typename T2>  void tfunc(long l, T2 t2) {   cout << "long and " << typeid(T2).name() << ": " << l << " " << t2 << endl;  }  template<>  void tfunc(short l, int i) {   cout << "long and int: " << l << " " << i << endl;  }  // 分别调用以上四个模板函数  tfunc(char('c'), char('c'));  tfunc(char('c'), int(10));  tfunc(long(10), char('c'));  tfunc(short(10), int(10));

函数模板实例化,让编译器生成指定类型的函数定义,不用写函数的实现,形式是 template void fun(type& t);

  // 函数模板  template<typename T>   void tfunc(const T& t) {   cout << "template: " << t << endl;  }    // 函数模板实例化,不用写函数的实现,编译器会生成该类型的模板具体化函数  template void tfunc<char>(const char& c);  

类模板

类模板可以指定默认模板参数(函数模板不可以),跟函数参数的默认值一样,必须从右向左连续赋值默认类型,如果实例化对象时又传递了类型,默认类型会被覆盖掉,跟函数参数是一样的
创建对象时需要传递模板参数列表,模板参数列表加在类名后面 ClassName< typename T > classN; 如果类的模板参数列

表有默认值,可以不传模板参数,但一定要加 <> 如 ClassName< > classN; 创建堆区对象的时候,所有的类名称后面都要加模板参数列表,如 ClassName< typename T >* classN = new ClassName< typename T>; 除了类内,其他地方出现 ClassName 的地方一般都要加模板参数列表

  template<typename T = int, typename Y = char> // 此处指定了模板默认参数,部分指定必须从右到左指定  class Test {  public:   Test(T t, Y y) : t(t), y(y) {   }   void tfunc();  private:   T t;   Y y;  };  template<typename T, typename Y> // 类模板的函数在类外实现,需要加上模板参数列表,但不需要加指定的默认模板参数  void Test<T, Y>::tfunc() { // 类外使用Test需要加模板参数   cout << t << " " << y << endl;  }  int n = 2;  double d = 2.1;  Test<int, double> test(n, d); // 此处如果使用默认模板参数可定义为 Test<> test(int(2), char('a'));  test.tfunc();  // 运行结果:2 2.1

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/483732.html

(0)
上一篇 2020年11月10日
下一篇 2020年11月10日

精彩推荐