c++ std::bind用法小结。

有些新东西,虽然不怎么使用这一套,但经常碰到,还是要了解一下。
小小试验,总结与分享:

#include<functional>
#include<iostream>
using namespace std;

class Cat
{
public:
    Cat() 
    {
        //001使用内部绑定;
        std::bind(&Cat::cry, this)();  
    }
    void cry() 
    {
        cout << "this is a cat" << endl;
    }
    void cry2()
    {
        cout << "I'm missing fishes" << endl;
    }

};

void print1()
{
    cout <<"print hello" << endl;
}

void print2(int a)
{
    cout <<"print "<< a << endl;
}

int selectMax(int a, int b)
{
    return a > b ? a : b;
}

void square(int num)
{
    cout <<"result:"<< num*num << endl;
}
int main()
{
    Cat c1;
    //002使用外部绑定;
    std::bind(&Cat::cry2, &c1)();

    //003不带参数;
    std::bind(print1)();

    //004带参数;
    std::bind(print2, std::placeholders::_1)(12);

    //005多重绑定
    auto selectMaxAndSquare = std::bind(square, std::bind(selectMax, std::placeholders::_1, std::placeholders::_2));
    selectMaxAndSquare(3, 6);

    system("pause");
    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值