成员模板:
成员模板不能为虚
template<class Type>
class Queue
{
public:
template<class It>
Queue(It beg,It end):head(0),tail(0)
{
Copy_elem(beg,end);
}
template<class Iter>
void assign(Iter,Iter);
private:
template<class Iter>
void Copy_elem(Iter,Iter);
};
成员声明的开头是自己的模板形参表,构造函数和成员函数各有一个模板类型形参,使用该类型形参作为函数形参的类型
当作类模板作用与外部定义成员模板时,必须包含两模板形参表
template<class T> template<class Iter>
void Queue<T>::assign(Iter beg.Iter end)
{
Destroy();
Copy_elem(beg,end);
}
首先是模板形参表template<class T>类模板的,第二个为template<class Iter>是成员模板的
template<class T>
class Foo
{
public:
static size_t count()
{
return ctr;
}
private:
static size_t ctr;
};
Foo<int> f1,f2,f3; //Each object share the same Foo<int>::ctr and Foo<int>::count members
Foo<string>f4,f5; //has static members Foo<string>::str and Foo<string>::count
Foo<int>类型的实例对象共享同一个static成员,而Foo<string>的对象共享另一个不同的成员
再类外部定义static数据成员,要给出类模板
template<class T>
size_t Foo<T>::str=0; //define and initialize ctr