1、类的模板的使用
类,由于没有参数,所以没有模板实参推导机制。
#include <stdexcept> template<typename T> class my_stack; template<typename T>
class list_node
{
T value;
list_node *next; list_node(T const &v, list_node *n) :
value(v), next(n){} friend class my_stack<T>;
}; template<typename T>
class my_stack
{
typedef list_node<T> node_type;
node_type *head; my_stack operator=(my_stack const&);
my_stack(my_stack const &s){} public:
my_stack() :head(){}
~my_stack()
{
while (!empty())
pop();
} bool empty(){ return head == ; }
T const& top() const throw(std::runtime_error)
{
if (empty())
{
throw std::runtime_error("Stack is empty");
}
return head->value;
} void push(T const &v)
{
head = new node_type(v, head);
} void pop();
}; template<typename T>//类成员函数的实现同样要放在和类声明相同的头文件里
void my_stack<T>::pop()
{
if (head)
{
node_type* tmp = head;
head = head->next;
delete tmp;
}
}
2、模板类的继承
template<typename T>
class count_stack : public my_stack<T>
{
typedef my_stack<T> base_type;
unsigned size; public:
count_stack() :base_type(), size(){}
void push(T const &v)
{
base_type::push(v);
size++;
} void pop()
{
if (size > )
{
base_type::pop();
size--;
}
} unsigned getSize() const { return size; }
};
如果是普通类以模板实例为基类,需要为基类模板给定明确的模板参数值。例如,
class another_stack:public my_stack<char>{}
3、异质链表
链表的类型为固定指向另一个类型节点的类,这就约束了链表中节点必须是同一类型,从而整个链表只能保存同一类型的数据。模板的一个特殊功能就是可以构造异质链表。构造的思路是以嵌套的形式,一会会进行演示。
这里只涉及到了异质链表的节点:
template<typename T, typename N>
struct hetero_node
{
T value;
N* next;
hetero_node(T const &v, N *n) :value(v), next(n){}
}; template<typename T, typename N>
hetero_node<T, N>* push(T const &v, N *n)
{
return new hetero_node<T, N>(v, n);
} template<typename T, typename N>
N* pop(hetero_node<T, N> *head)
{
N *next = head->next;
delete head;
return next;
}
使用异质链表结构构造三元组:
typedef hetero_node<int, void> node0;
typedef hetero_node<char, node0> node1;
typedef hetero_node<std::string, node1> node2; node2 *p2 = push(std::string("Awesome"),
push('w',
push(, (void*)NULL))); pop(pop(pop(p2)));
构建链表时,一个节点的指针,就代表了整个链表,所有的操作都采用一种嵌套的形式,可以体现一种新的设计思路。
next指针占据额外的开销,有没有办法节省这种开销?
template<typename T, typename N>
struct tuple
{
T value;
N next;
tuple(T const &v, N const &n) :value(v), next(n){}
}; template<typename T, typename N>
tuple<T, N> push(T const &v, N const &n)
{
return tuple<T, N>(v, n);
} typedef tuple<int, char> tuple0;
typedef tuple<float, tuple0> tuple1;
typedef tuple<std::string, tuple1> tuple2; tuple2 t = push(std::string("test"),
push(1.0f,
push(, 'a')));
当然你也可以用类来实现tuple,但是这样的元组的元素个数是固定的:
template<typename T0, typename T1, typename T2>
struct tuple3
{
T0 v0;
T1 v1;
T2 v2;
tuple3(T0 const &_v0, T1 const & _v1, T2 const & _v2) :
v0(_v0), v1(_v1), v2(_v2){}
};
总结:异质链表摆脱具体数据类型的束缚。
4、成员函数模板
如果类是一个模板,以我们前面的my_stack为例:
template<typename T>
void my_stack<T>::pop()
{
if (head)
{
node_type* tmp = head;
head = head->next;
delete tmp;
}
}
如果类不是一个模板,但成员函数是一个模板:
struct normal_class
{
int value;
template<typename T>
void set(T const &v)
{
value = int(v);
}
};
如果类和成员函数都是模板,该怎么在类外定义:
template<typename N>
struct a_class_template
{
N value; template<typename T>
void set(T const& v)
{
value = N(v);
} template<typename T>
T get();
}; template<typename N> template<typename T>
T a_class_template<N>::get()
{
return T(value);
}
5、类模板的静态成员
template<typename T>
struct the_class
{
static int id;
the_class(){ id++; }
}; template<typename T> int the_class<T>::id = ; void call1()
{
the_class<int> c;
printf("static id:%d\n", c.id);
} void call2()
{
the_class<int> c;
printf("static id:%d\n", c.id);
}
输出结果:

编译call1.cpp和call2.cpp会生成两个the_class的实例,会拥有两个the_class<int>::id的分配内存地址,但是在链接时,链接器将随机选择一个目标中的空间作为最终的存储空间,从而使不同目标文件中的多个等价模板实例共享同一套静态成员存储空间。