C++函数指针和std:function、std:bind

1.C++函数指针

指向普通函数:

语法:函数类型+(指针)(参数类型)= 所指函数

bool (*p)(int) = func;

bool (&p)(int) = func;

p(10);

指向类成员函数:

#include "iostream"
using namespace std;

class Func
{
public:
    void operator()()
    {
        cout << "operater()" << endl;
    }
};

class Base
{
public:
    int a;
    int b;

    void display()
    {
        cout << "Base Display" << endl;
    }

    static void count()
    {
        cout << "Base Count" << endl;
    }
};

int main()
{
    void (Base::*p)() = &Base::display;
    Base base;
    (base.*p)();//调用完无需绑定对象

    void (*p1)() = &Base::count;
    p1();//调用时无需绑定对象
}

2.std:function

分为五种情况:

        封装普通函数;

        封装仿函数;

        封装类函数;

        封装静态类函数;

        封装lambda表达式

#include "iostream"
#include "functional"
using namespace std;

class Base
{
public:
    int a;
    int b;

    void display()
    {
        cout << "Base Display" << endl;
    }

    static void count()
    {
        cout << "Base Count" << endl;
    }
};

class Func2
{
public:
    void operator()()
    {
        cout << "operater()" << endl;
    }
};

void Func1(const string str)
{
    cout << str + str << endl;
}


int main()
{
    //封装普通函数
    function<void(const string)>funcA = Func1;
    funcA("aabb");

    //封装仿函数
    function<void()>funcB = Func2();
    funcB();

    //封装类函数
    Base base;//创建一个Base类的对象
    function<void()>funcC = bind(&Base::display,&base);
    funcC();

    //封装静态类函数
    function<void()>funcD = Base::count;
    funcD();

    //封装lambda表达式
    string str;
    auto f = [&](int val){
        str = "值:";
        cout << str << " " << val << endl;
    };
    function<void(int)>funcE = f;
    funcE(30);
}

3.std:bind

std:bind——将函数或可调用对象与特定参数绑定,从而创建一个新的可调用对象;

#include "iostream"
#include "functional"

using namespace std;

void hello(string str)
{
    cout << "hello " << str << endl;
}

class Chello
{
public:
    void h(string str)
    {
        cout << "hello " << str << endl;
    }
};

class likehello
{
public:
    void operator()(string str)
    {
        cout << "hello " << str << endl;
    }
};

int main()
{
    //bind绑定普通函数和参数
    auto fhello = bind(&hello,"Tim");
    fhello();//调用被绑定的hello函数就不用传入参数

    //bind绑定类函数和参数
    Chello c1;
    auto chello = bind(&Chello::h,&c1,"cook");
    chello();

    //bind绑定仿函数
    auto lhello = bind(likehello(),"TimCook");
    lhello();

}

### C++ 中 `std::function` `std::bind` 的用法及区别 #### 定义与基本概念 `std::function` 是一种通用多态函数封装器,可以存储任何可调用对象(如普通函数、lambda 表达式、绑定表达式或其他函数对象)。其灵活性使得它能够作为回调机制的一部分,在事件驱动编程其他场景下非常有用。 ```cpp #include <functional> #include <iostream> void simpleFunction(int a) { std::cout << "Value is: " << a << '\n'; } int main() { // 创建一个 std::function 对象并赋值给简单函数 std::function<void(int)> f = simpleFunction; f(42); } ``` 另一方面,`std::bind` 提供了一种方式来创建新的可调用对象,这些新对象可以通过固定某些参数或将多个可调用实体组合在一起的方式简化现有可调用目标的接口。通过 bind 可以提前设置部分参数,从而减少后续调用时传递相同参数的需求[^1]。 ```cpp #include <functional> #include <iostream> class MyClass { public: void memberFunc(int i, double d) const { std::cout << "Integer value: " << i << ", Double Value:" << d << "\n"; } }; int main(){ using namespace std::placeholders; MyClass obj; // 绑定成员函数到特定实例,并预先设定第一个参数 auto boundMember = std::bind(&MyClass::memberFunc, &obj, _1, 3.14); // 调用已绑定的对象只需要提供剩余未固定的参数即可 boundMember(7); } ``` #### 主要差异点 - **目的不同**: `std::function` 更侧重于作为一个容器去保存各种型的可调用对象;而 `std::bind` 则专注于调整已有可调用对象的行为模式,比如预设一些输入参数。 - **使用场合的区别**: 当需要定义一个能接受多种不同型但签名兼容的函数指针的地方时,应该优先考虑使用 `std::function`; 如果想要改变某个具体函数或方法的工作方式,则可以选择 `std::bind`. - **性能考量**: 在大多数情况下,直接使用 lambda 表达式可能比使用 `std::bind` 效率更高,因为后者可能会引入额外的小型分配开销以及间接层。然而对于复杂情况下的参数绑定操作来说,`std::bind` 还是有它的优势所在[^2].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值