C++程序设计兼谈对象模型(侯捷)——课程笔记(三)

本篇说的是function-like classes,也就是行为像函数的class。小括号()学名叫做function call operator,也就是函数调用运算符,重载了operator()的class就是function-like class。下面是一些标准库里面的实例:

template <class T>
struct identity : public unary_function<T, T>{
    const T&
    operator() (const T& x) const { return x; }
};

template <class Pair>
struct select1st : public unary_function<Pair, typename Pair::first_type>{
    const typename Pair::first_type&
    operator() (const Pair& x) const
    { return x.first; }
};

template <class Pair>
struct select2nd : public unary_function<Pair, typename Pair::second_type>{
    const typename Pair::second_type&
    operator() (const Pair& x) const
    { return x.second; }
};

除了重载了operator()以外,我们发现这些class还继承了一些看起来很“奇怪”的base class,下面看一下这些base class的例子:

template <class Arg, class Result>
struct unary_function{
    typedef Arg argument_type;
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值