应用器 & 操纵器

      操纵器(Manipalator),以某种方式作用于它的参数所表示的数据;应用器(Applicator),是一个重载操作符,它的操作数是一个可操纵的值和一个将作用于这个值得操纵器。

 template <class stype,class vtype >
class fcn_obj
{
public:
  fcn_obj(stype (*f)(stype&,vtype),vtype v):
            func(f),val(v)
  {
  }
  stype& operator()(stype& s)const
  {
    return (*func)(s,val);
  }
private:
  stype& (*func)(stype&,vtype),vtype v);
  vtype val;
};

template <class stype,class vtype>
stype& operator<<
(stype& ofile,const fcn_obj<stype,vtype>& im)
{
   return im(ofile);
}

fun_obj<ostream long>
hexconv(long n)
{
   ostream (*my_hex)(ostream&,long)=hexconv;
   return fcn_obj<ostream,long>(my_hex,n);
}

 

上面的代码来自Ruminations On C++,iostream库中使用了这个设计,使得cout<<"Hello,World!"<<endl;用起来很舒服,终于知道了endl为什么叫做操纵器了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值