• C++ 函数对象(Function Object)是什么?C++重载小括号()是什么作用?


    在了解重载()之前,首先要理解C++里面的函数对象的概念。

    1. 什么是函数对象?

    A FunctionObject type is the type of an object that can be used on the left of the function call operator.

    即:函数对象类型是可以在函数调用操作符(PS:就是小括号())左侧使用的对象类型
    根据文档可以总结出函数对象的几种类型:

    1. 函数或者函数引用(需要隐式转换)
    2. 函数指针
    3. < functional >头文件中定义的所有函数对象(如less,greater)
    4. < functional >头文件中一些函数的返回值

    Notes
    Functions and references to functions are not function object
    types, but can be used where function object types are expected due to
    function-to-pointer implicit conversion.

    Standard library
    All pointers to functions satisfy this requirement.
    All function objects defined in < functional >
    Some return types of functions of < functional >

    此外还有:
    5. lambda表达式(C++11之后)
    6. 重载了函数调用运算符()的类的对象

    在C++中,自定义的比较函数中:常见的传入函数名,less或者greater对象都属于这种函数对象。但是,类似下面这种是做了隐式类型转换的。(具体转换流程没去看,有清楚的可以留言哈)

    // 传入函数名
    vector<int> vec;
    bool cmp(const pair<int, int>& A. const pair<int, int>& B) {
    	return A.second < B.second;
    }
    sort(vec.begin(), vec.end(), cmp);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    下面是其他对于函数对象的几种用法的实例(函数指针,重载了函数调用运算符()的类的对象
    ,lambda表达式)

    #include 
     
    void foo(int x) { std::cout << "foo(" << x << ")\n"; }
     
    int main()
    {
        void(*fp)(int) = foo;
        fp(1); // calls foo using the pointer to function
     
        struct S {
            void operator()(int x) const { std::cout << "S::operator(" << x << ")\n"; }
        } s;
        s(2); // calls s.operator()
        s.operator()(3); // the same
     
        auto lam = [](int x) { std::cout << "lambda(" << x << ")\n"; };
        lam(4); // calls the lambda
        lam.operator()(5); // the same
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2. 重载函数调用运算符()小括号

    小括号是函数调用运算符。
    重载()小括号,也就是重载函数调用运算符。

    当用户定义类重载函数调用operator()时,它将成为FunctionObject类型。而FunctionObject类型可以当作一个函数一样来调用,这种类型的对象可以在函数调用表达式中使用:

    这样说可能较为晦涩,但是下面的实例应该很好看懂:

    // An object of this type represents a linear function of one variable a * x + b.
    struct Linear
    {
        double a, b;
     
        double operator()(double x) const
        {
            return a * x + b;
        }
    };
     
    int main()
    {
        Linear f{2, 1};  // Represents function 2x + 1.
        Linear g{-1, 0}; // Represents function -x.
        // f and g are objects that can be used like a function.
     
        double f_0 = f(0);
        double f_1 = f(1);
     
        double g_0 = g(0);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    重载函数调用运算符()之后的类就是函数对象(FunctionObject)类,就有了函数对象(FunctionObject)类的功能,其实例化的对象,就是本文第一节所介绍的。

    参考资料:

    1. https://en.cppreference.com/w/cpp/named_req/FunctionObject
    2. https://en.cppreference.com/w/cpp/language/operators
      第二个链接参看其中的Function call operator这一节,这两个文档写的非常好,强烈推荐
  • 相关阅读:
    CANoe-如何基于CAN协议在诊断控制台诊断通信(5000字保姆级教程)
    医学图像分割之--Tversky Loss
    Qt软件发布(版本信息,Release版程序,代码打包,制作安装包)
    如何在IDEA上使用Git克隆下载创建好的项目和分支、提交项目
    ubuntu系统简单尝试
    24 | ping 命令的使用
    智能合约 | solidity必会的开发技巧2--合约升级(修改合约功能)
    android源码编译环境准备(1)
    保险代理人快速提升展业,应该从什么方向入手?
    Echarts实现半圆形饼图,Echarts实现扇形图
  • 原文地址:https://blog.csdn.net/Sansipi/article/details/127876449