zoukankan      html  css  js  c++  java
  • std::function与std::bind 函数指针

    function模板类和bind模板函数,使用它们可以实现类似函数指针的功能,但却却比函数指针更加灵活,特别是函数指向类 的非静态成员函数时。

    std::function可以绑定到全局函数/类静态成员函数(类静态成员函数与全局函数没有区别),如果要绑定到类的非静态成员函数,则需要使用std::bind


    #include <iostream> #include <functional> using namespace std; typedef std::function<void()> fp; void g_fun() { cout << "g_fun()" << endl; } class A{ public: static void A_fun_static() { cout << "A_fun_static()" << endl; } void A_fun() { cout << "A_fun()" << endl; } void A_fun_int(int i) { cout << "A_fun_int()" << i << endl; } //非静态类成员,因为含有this指针,所以需要使用bind void init() { fp fp1 = std::bind(&A::A_fun, this); fp1(); } void init2() { typedef std::function<void(int)> fpi; //对于参数要使用占位符 std::placeholders::_1 fpi f = std::bind(&A::A_fun_int, this, std::placeholders::_1); f(5); } }; int main(){ //绑定到全局函数 fp f2 = fp(&g_fun); f2(); //绑定到类静态成员函数 fp f1 = fp(&A::A_fun_static); f1(); A().init(); A().init2(); return 0; }

    CCCallFunc CCCallFuncN CCCallFuncND的区别和使用 

    http://www.cnblogs.com/newlist/archive/2013/07/20/3203059.html

  • 相关阅读:
    循环链表结构
    复杂的权衡之时间、空间和单链表结构
    单链表操作之删除
    单链表操作之插入
    单链表操作之替换
    单链表操作之搜索
    文件系统的原理
    类加载的三种方式比较
    shell中awk printf的用法
    推荐网站
  • 原文地址:https://www.cnblogs.com/as3lib/p/3889747.html
Copyright © 2011-2022 走看看