读书笔记--模板(3)

成员模板:
成员模板不能为虚
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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值