zoukankan      html  css  js  c++  java
  • c++ 可调用对象

    参考:原文:C++中的各种可调用对象 (更舒服的排版:C++中的各种可调用对象

    这篇博客真的总结的很好,强烈推荐

    --------------------------------------------------------一些笔记---------------------------------------------------------------------------

    这里也可以不指明lambda的返回类型,即去掉   ->int

    感受一下lambda和普通函数定义的差别:

    int sum(int x, int y){return x+y;}
    auto sum =[](int x, int y){return x+y;}
    

    这里对类成员函数的支持使用std::bind,因为类成员函数的参数列表中有this指针

    具体的使用 std::function 描述所有可调用实体code:

    #include <iostream>
    #include <functional>
    
    std::function<int(int, int)> SumFunction;
    
    // 普通函数
    int func_sum(int a, int b)
    {
        return a + b;
    }
    
    class Calcu
    {
    public:
        int base = 20;
        // 类的成员方法,参数包含this指针
        int class_func_sum(const int a, const int b) const { return this->base + a + b; };
        // 类的静态成员方法,不包含this指针
        static int class_static_func_sum(const int a, const int b) { return a + b; };
    };
    
    // 仿函数
    class ImitateAdd
    {
    public:
        int operator()(const int a, const int b) const { return a + b; };
    };
    
    // lambda函数
    auto lambda_func_sum = [](int a, int b) -> int { return a + b; };
    
    // 函数指针
    int (*func_pointer)(int, int);
    
    int main(void) 
    {
        int x = 2; 
        int y = 5;
    
        // 普通函数
        SumFunction = func_sum;
        int sum = SumFunction(x, y);
        std::cout << "func_sum:" << sum << std::endl;
    
        // 类成员函数
        Calcu obj;
        SumFunction = std::bind(&Calcu::class_func_sum, obj, 
            std::placeholders::_1, std::placeholders::_2); // 绑定this对象
        sum = SumFunction(x, y);
        std::cout << "Calcu::class_func_sum:" << sum << std::endl;
    
        // 类静态函数
        SumFunction = Calcu::class_static_func_sum;
        sum = SumFunction(x, y);
        std::cout << "Calcu::class_static_func_sum:" << sum << std::endl;
    
        // lambda函数
        SumFunction = lambda_func_sum;
        sum = SumFunction(x, y);
        std::cout << "lambda_func_sum:" << sum << std::endl;
    
        // 带捕获的lambda函数
        int base = 10;
        auto lambda_func_with_capture_sum = [&base](int x, int y)->int { return x + y + base; };
        SumFunction = lambda_func_with_capture_sum;
        sum = SumFunction(x, y);
        std::cout << "lambda_func_with_capture_sum:" << sum << std::endl;
    
        // 仿函数
        ImitateAdd imitate;
        SumFunction = imitate;
        sum = SumFunction(x, y);
        std::cout << "imitate func:" << sum << std::endl;
    
        // 函数指针
        func_pointer = func_sum;
        SumFunction = func_pointer;
        sum = SumFunction(x, y);
        std::cout << "function pointer:" << sum << std::endl;
    
        getchar();
        return 0;
    }
    View Code

    注意一下:

    #include <iostream>
    #include <functional>
    
    std::function<int(int, int)> ComputeFunction;
    int (*compute_pointer)(int, int);
    
    int compute1(int x, int y, ComputeFunction func) {
        // do something
        return func(x, y);
    }
    
    int compute2(int x, int y, compute_pointer func)
    {
        // do something
        return func(x, y);
    }
    // 调用方式参考上面关于函数指针和std::function的例子
    View Code

    #include <iostream>
    
    // 定义标准的回调接口
    class ComputeFunc
    {
    public:
        virtual int compute(int x, int y) const = 0;
    };
    
    // 实现回调接口
    class ComputeAdd : public ComputeFunc
    {
    public:
        int compute(int x, int y) const override { return x + y; }
    };
    
    int compute3(int x, int y, const ComputeFunc& compute)
    {
        // 调用接口方法
        return compute.compute(x, y);
    }
    
    // 调用方法如下
    int main()
    {
        ComputeAdd add_func; // 创建一个调用实例
        int sum = compute3(3, 4, add_func); // 传入调用实例
    }
    View Code

    真的总结的很好,笔记基本上全抄了一遍...

  • 相关阅读:
    Java 代理模式
    ReentrantLock 详解
    Java线程池详解
    ConcurrentHashMap 解读
    CountDownLatch/CyclicBarrie用法记录
    微信接入笔记记录
    iOS设计模式
    iOS设计模式
    iOS设计模式
    iOS设计模式
  • 原文地址:https://www.cnblogs.com/exciting/p/11160466.html
Copyright © 2011-2022 走看看