10.9.2 std::function 非OO的多态实现 Page185~187

源代码:

#include <iostream>
#include <functional>
#include <list>

using namespace std;

//使用function模板类定义一个类型,
//该类型要求作为T的
//函数类型是参数是string,返回值是void
typedef std::function <void (std::string)>
                            IntroductionFunction;

struct Person
{
    //参数是一个IntroductionFunction类型的对象引用
    Person(IntroductionFunction& f)
        : _introduction(f)
    {
    }

    void Introduction(std::string const& name) const
    {
        this->_introduction(name);
    }

private:
    //成员是一个IntroductionFunction类型的对象
    IntroductionFunction _introduction;
};

//普通人的介绍
//函数类型是参数是string,返回值是void,
//可用来构造IntroductionFunction类型的对象
void NormalIntroduction(std::string const& name)
{
    cout << "我是:" << name << endl;
}

//美女的介绍
//参数是string,返回值是void,
//可用来构造IntroductionFunction类型的对象
void BeautyIntroduction(std::string const& name)
{
    cout << "我是大美女:" << name << endl;
}

///超级大美女的介绍
struct SuperBeautyIntroduction
{
    SuperBeautyIntroduction()
        : _count(0)
    {

    }
    //重载(),参数是string,这样SuperBeautyIntroduction的对象
    //可以作为函数对象使用,函数类型是void (std::string),
    //所以SuperBeautyIntroduction的对象也可以用来构造
    //IntroductionFunction对象
    void operator () (std::string const& name)
    {
        if(_count < 1)
        {
            BeautyIntroduction(name);
        }
        else
        {
            cout << "你们这些媒体烦不烦嘛,我这都第"
                 << _count + 1 << "次自我介绍了!" << endl;
        }

        ++ _count;
    }
private:
    int _count;//介绍次数
};

void test()
{
    /* IntroductionFunction是std::function <void (std::string)>类型,
    NormalIntroduction是void (std::string)类型的函数
    所以n_introduce实际上是以NormalIntroduction为参数构造出来的一个对象*/

    //以普通人自我介绍的方式构造一个对象
    //使用NormalIntroduction作为参数构造一个IntroductionFunction对象n_introduce
    IntroductionFunction n_introduce(NormalIntroduction);
    //使用对象n_introduce构造一个Person对象
    Person p1(n_introduce); ///构造一个普通人对象
    p1.Introduction("李师师"); ///普通人开始介绍自己
    p1.Introduction("李师师");
    //使用BeautyIntroduction作为参数构造一个IntroductionFunction对象b_introduce
    IntroductionFunction b_introduce(BeautyIntroduction);
    Person p2(b_introduce); ///构造一个美女对象
    p2.Introduction("王熙凤");
    p2.Introduction("王熙凤");
    //sb是个函数对象,函数类型是void (std::string)
    SuperBeautyIntroduction sb; ///一个超级大美女对象
    IntroductionFunction sb_introduce(sb); ///sb是个函数对象
    Person p3(sb_introduce);
    p3.Introduction("凤姐");
    p3.Introduction("凤姐");

}
void demo_polymorphism_without_virtual()
{
    //list的元素类型是pair <Person, string>
    std::list <std::pair <Person, std::string>> lst;
    /**< IntroductionFunction的参数是string类型,返回值是void类型 */
    /**< IntroductionFunction是Person类的一个成员 */
    //n_introduce是个IntroductionFunction类型的函数
    /**< 相当于IntroductionFunction n_introduce = NormalIntroduction */
    IntroductionFunction n_introduce(NormalIntroduction);
    /**< 相当于IntroductionFunction b_introduce = BeautyIntroduction; */
    IntroductionFunction b_introduce(BeautyIntroduction);

    SuperBeautyIntroduction sb;  //一个超级大美女对象
    IntroductionFunction sb_introduce(sb); ///这里不用ref, 这里的sb是函数对象sb()
    /**< 将Person和名字绑定到一起构成一个pair,然后存储到lst里面 */
    lst.push_back(std::make_pair(Person(n_introduce), "玫露"));
    lst.push_back(std::make_pair(Person(n_introduce), "梦露"));
    lst.push_back(std::make_pair(Person(sb_introduce), "露露"));

    for(auto it = lst.begin(); it != lst.end(); ++ it)
    {
        //每人自我介绍两次:
        it->first.Introduction(it->second);
        it->first.Introduction(it->second);
    }
}

int main()
{
    test();
    cout << "----------" << endl;
    demo_polymorphism_without_virtual();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值