Command设计模式2

2014-11-07 星期五 22:52:07 

接1,先说下typename和class的差异。

  修饰template形参时,两者无差异

  重定义(typedef)或要使用template内部的类型时,在此前面只能用typename来表明template::inherent_type,例如(typename ParentFunctor::ResultType)

1、代码流程

Functor< void, TYPELIST_2(int , double) > cmd(f);

  先看构造函数

1
2
3
template < typename  Fun>
Functor( const  Fun& fun)
:spImpl_( new  FunctorHandler<Functor, Fun>(fun))
  1. FunctorImpl()->FunctorHandler(const Fun& fun):fun_(fun)

    把fun传给了FunctorHandler::fun_。


  2. :spImpl_(new FunctorHandler<Functor, Fun>(fun))

    spImpl_初始化了,Functor构造也完成了。


  3. cmd(f)

    根据< void, TYPELIST_2(int , double) >自动调用仿函数Functor::operator()->FunctorImpl::operator()->FunctorHandler::operator()->FunctorHandler::fun_

  4. 总结​

只要Functor< void, TYPELIST_2(int , double) > cmd(f)中<>的类型具现了,编译器就会自动匹配最优的Functor::operator()仿函数。其他没有用到的仿函数就不会编译。举个浅显的例子。鞋店并不知道你的脚的大小等,但它提供了足够多的型号来供你选择。你选中了合适的鞋子后,其他的鞋子都与你无关了,无论它们是一步登天的仙鞋,还是步履维艰的妖鞋子,都与你无关了。




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值