20200229
Plan
60min: cpp template
Notes
- 两种模版: 函数模版+类模版
- 每个模版都含有模版参数,而模版参数分为模版类型参数,和模版非类型参数,前者很好理解,后者的意思是模版的参数可以是值而不是类型。
- 模版的定义必须可见,不然模版本身无法编译(比如编译器无法知道模版参数的要求),模版实例化的时候,所有相关参数也必须可见,否则就无法正确实例化,因此一般来说,模版的头文件包含了定义。
- 在类代码中,可以使用其他模版,并且可以简化自己的模版类的写法(无需指定模版参数)。
练习代码:
#include <iostream>
template <typename elemType> class ListItem;
template <typename elemType> class List {
public:
List<elemType>();
List<elemType>(const List<elemType> &);
List<elemType> &operator=(const List<elemType> &);
~List();
void insert(ListItem<elemType> *ptr, elemType value);
private:
ListItem<elemType> *front, *end;
};
int main() { return 0; }
More
syntax: typedef typename <type> alias?