类模板声明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
在这些形式中,TypeParam是命名将要存储在容器类SomeClass中的数据的类型的通用类型參数,而关键词typename能够被替换为class。
注意
1.关键词template规定了后面接着的是一个类的模式,而不是一个实际的类声明
2.在类型形參列表中关键词tpyename和class能够互换使用
3.和规则类的函数成员不同,在使用类模板时,编译器必须可以找到他的函数成员的定义。一种通常的方法是将全部函数定义从实现文件ClassName.cpp中移到ClassName.h中类声明后面。还有一种方法是将函数定义放在单独的文件里然后再ClassName.h的最后#include这个文件。
一个类模板不过描写叙述怎样依据给定的类型那个构建不同的类的一个模式,这个创建一个类的过程被称为实例化。这是通过附加一个实际类型到对象定义中的类名上来实现的:
1 | |
比如,能够使用以下的定义来实例化Stack这个类模板:
1 2 | |
当编译器处理这些声明时,他将产生两个不同的Stack类(两个实例),一个是使用char取代了StackElement,还有一个是使用double取代了StackElement。第一个类中的构造函数将构造charStack为一个空的字符型的栈,而第二个类中的构造函数将构造dubStack为一个空的double型的栈。
有3条重要的规则规定了怎样创建类模板:
1.全部定义在类声明之外的操作都必须是模板函数
2.不论什么将一个模板类的名作为类型的使用都必须參数化
3.当使用类模板时,编译器必须可以找到它的操作的定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
一个Stack类模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
相应的cpp文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
|