【c++11】std::bind使用

本文介绍C++标准库中std::bind的使用方法,包括如何绑定全局函数、静态成员函数及成员函数,并通过具体示例展示了不同绑定方式的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1.绑定全局函数
[
#include <iostream>
#include <functional>

typedef std::function<void(int, int)> callback;

void fun(int n1, int n2)
{
    OutputDebugString(L"fun");
}

使用:
std::function<void(int, int)> ss1 = std::bind(fun, std::placeholders::_1, std::placeholders::_2);
ss1(1,2);

std::function<void(int)> ss2 = std::bind(fun, std::placeholders::_1, 2);
ss2(1);

std::function<void(int)> ss3 = std::bind(fun, 1, std::placeholders::_1);
ss3(2);


std::function<void()> ss4 = std::bind(fun, 1, 2);
ss4();

auto ss5 = std::bind(fun, std::placeholders::_1, std::placeholders::_2);
ss5(1, 2);

std::function<void(int, int)> ss6 = std::bind(fun2, std::placeholders::_1, 2, std::placeholders::_2);
ss6(1,3);

]

2.绑定static成员函数
[

#include <iostream>
#include <functional>

typedef std::function<void(int, int)> callback;

void fun(int n1, int n2)
{
    OutputDebugString(L"fun");
}

class CTest
{
public:
    CTest() 
    {
    }

    ~CTest()
    {

    }

    void test_fun(int n1, int n2)
    {
        OutputDebugString(L"test_fun");
    }

    static void static_test_fun(int n1, int n2)
    {
        OutputDebugString(L"static_test_fun");
    }

};

使用:

//static静态成员
callback ss7 = std::bind(CTest::static_test_fun, std::placeholders::_1, std::placeholders::_2);
ss7(1, 2);
]

3.绑定成员函数
[
#include <iostream>
#include <functional>

typedef std::function<void(int, int)> callback;

class CTest
{
public:
    CTest() 
    {
    }

    ~CTest()
    {

    }

    void test_fun(int n1, int n2)
    {
        OutputDebugString(L"test_fun");
    }

    static void static_test_fun(int n1, int n2)
    {
        OutputDebugString(L"static_test_fun");
    }

};

使用:

//成员
CTest test;

callback ss8 = std::bind(&CTest::test_fun, &test, std::placeholders::_1, std::placeholders::_2);
ss8(1, 2);
]


---------------------------------------------------
完整代码:

#include <iostream>
#include <functional>

typedef std::function<void(int, int)> callback;

void fun(int n1, int n2)
{
    OutputDebugString(L"fun");
}

void fun2(int n1, int n2, int n3)
{
    OutputDebugString(L"fun2");
}

class CTest
{
public:
    CTest() 
    {
    }

    ~CTest()
    {

    }

    void test_fun(int n1, int n2)
    {
        OutputDebugString(L"test_fun");
    }

    static void static_test_fun(int n1, int n2)
    {
        OutputDebugString(L"static_test_fun");
    }

};

使用:

//std::bind使用
    std::function<void(int, int)> ss1 = std::bind(fun, std::placeholders::_1, std::placeholders::_2);
    ss1(1,2);

    std::function<void(int)> ss2 = std::bind(fun, std::placeholders::_1, 2);
    ss2(1);

    std::function<void(int)> ss3 = std::bind(fun, 1, std::placeholders::_1);
    ss3(2);


    std::function<void()> ss4 = std::bind(fun, 1, 2);
    ss4();

    auto ss5 = std::bind(fun, std::placeholders::_1, std::placeholders::_2);
    ss5(1, 2);

    std::function<void(int, int)> ss6 = std::bind(fun2, std::placeholders::_1, 2, std::placeholders::_2);
    ss6(1,3);

    //static静态成员
    callback ss7 = std::bind(CTest::static_test_fun, std::placeholders::_1, std::placeholders::_2);
    ss7(1, 2);

    //成员
    CTest test;

    callback ss8 = std::bind(&CTest::test_fun, &test, std::placeholders::_1, std::placeholders::_2);
    ss8(1, 2);


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kevin--你不知道的事

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值