模板

模板是一种参数化多态性工具

一.函数模板

1 template<class T1, class T2>
2 T1 max(T1 x, T2 y)
3 {
4     return x > y ? x : (T1)y;
5 }

函数模板是对一组函数的描述,不是一个实实在在的函数,编译时不产生任何可执行代码。

当编译系统在程序中,发现有与函数模板中相匹配的函数调用时,便生成一个重载函数。

 1 #include<iostream>
 2 using namespace std;
 3 
 4 template<class T>
 5 T max(T x, T y)
 6 {
 7     return x > y ? x : y;
 8 }
 9 
10 int main()
11 {
12     int a = 10; float b = 1.1;
13     cout << max(a, b) << endl;// error,必须类型相同
14 
15     return 0;
16 }
1 T max(T1 a, T2 b);
2 
3 Max(1.2, 3.4);//ok
4 Max(4, 5);//ok
5 Max(1.2, 3);//ok

二.重载

1 template<class T>
2 T max(T a, T b);
3 
4 template<class T>
5 T max(T a, int n);
6 
7 int max(int a, int b);
 1 //先找参数完全匹配的普通函数
 2 //再找完全匹配的模板
 3 //经过动态类型转换的普通函数
 4 
 5 T max(T a, T b) { cout << 1; }
 6 T max(T1 a, T2 b) { cout << 2; }
 7 double max(double a, double b) { cout << 3; }
 8 
 9 Max(1.2, 3.4);//3
10 Max(4, 5);//1
11 Max(1.2, 3);//2

三.类模型

1.

 1 template<class T>
 2 class XX
 3 {
 4     T x;
 5     T getX(){}
 6 };
 7 int main()
 8 {
 9     XX<int> x;
10 }
1 void XX<T>::setX(T a)
2 {
3 
4 }

模板可拥有多个类参数

1 template<class T1, class T2, class T3>
2 class S
3 {
4 public:
5     T2 m(T3 p){}
6 private:
7     T1 x;
8 };

2.实例化

类模板是调用函数时实参的类型来确定具体类型

Array<int> a(50); // ok

编译器先用int替代类模板定义中的类型参数T,生成一个类型确定的类,再利用这个类创建对象a。

类模板可作为一种数据类型出现在参数表中。

void xx(const Array<T> &a);

3.类模板可以有非类参数,函数型参数

 1 template<class T, int X, int Y>
 2 class C {};
 3 
 4 
 5 template<class T, int a>
 6 class C
 7 {
 8     C()
 9     {
10         x = a;
11     }
12 
13 private:
14     T x;
15 };
16 
17 C<double, 8> c;
 1 template<class T, int SIZE>
 2 class CArray
 3 {
 4 private:
 5     T array[SIZE];
 6 public:
 7     void Print()
 8     {
 9         for (int i = 0; i < SIZE; i++)
10         {
11             cout << array[i] << endl;
12         }
13     }
14 };
15 
16 CArray<double, 40> a1;
17 CArray<int, 50> a2;

4.模板类可以继承也可以派生

 1 template<class T>
 2 class A{};
 3 
 4 A<int> aint;
 5 A<double> adouble;
 6 //不存在A这个基类,没有公共基类,无法多态
 7 
 8 class Abase{};
 9 
10 template<T> class A: public Abase{};
11 A<int> aInt;
12 A<double> aDouble;
13 //可多态
1 template<T> class A{static char a};
2 A<int> aint1, aint2;
3 A<double> adouble1, adouble2;
4 //aint1与adouble1并没有一个共同静态成员
5 //aint1与aint2有共同静态成员

5.同一个类模板的2个模板类不兼容

1 Pair<int, double> *p;
2 Pair<int, string> a;
3 p = &a;//error

四.派生

1.类模板从类模板

 1 template<class T1, class T2>
 2 class A
 3 {
 4 public:
 5     T1 v1;
 6     T2 v2;
 7 };
 8 
 9 template<class T1, class T2>
10 class B : public A<T1, T2>
11 {
12 public:
13     T1 v3;
14     T2 v4;
15 };
16 
17 int main()
18 {
19     return 0;
20 }

2.类模板从模板类

 1 template<class T1, class T2>
 2 class A
 3 {
 4 public:
 5     T1 v1;
 6     T2 v2;
 7 };
 8 
 9 template<class T>
10 class B : public A<int, double>
11 {
12 public:
13     T a;
14 };
15 
16 int main()
17 {
18     return 0;
19 }

3.类模板从普通类

 1 class A
 2 {
 3 };
 4 
 5 template<class T>
 6 class B : public A
 7 {
 8 public:
 9     T a;
10 };
11 
12 int main()
13 {
14     return 0;
15 }

4.普通类从模板类

 1 template<class T>
 2 class A
 3 {
 4 public:
 5     T a;
 6 };
 7 
 8 class B : public A<int>
 9 {
10 };
11 
12 int main()
13 {
14     return 0;
15 }
原文地址:https://www.cnblogs.com/wanderingzj/p/5296298.html