重载运算与类型转换

本文介绍了C++中可调用对象的五种类型,包括函数、函数指针、lambda表达式、bind创建的对象和重载了函数调用运算符的类。通过`std::function`模板,可以将这些具有相同调用形式的对象进行统一管理。文中展示了如何创建和使用`std::function`,以及不同类型的可调用对象作为参数传递的例子。此外,还提及了`bind`的使用方法。
摘要由CSDN通过智能技术生成

1. 输入和输出运算符

2. 函数调用运算符

3. 标准库定义的函数对象

4. lambda 表达式


4.2 作用


  在 C++ 中,lambda 表达式的捕获功能可以很方便地把一个对象的数据成员和函数成员传递出去。在类中定义的 lambda 表达式,只要捕获 this,就可以直接使用该类的成员,非常直观。如果使用函数指针,则只能把 this 作为参数传递出去,在接收者中使用 this->xxx 引用 this 的成员。

  该例使用非成员函数 add 处理类 Example 对象的数据:把 a 与 b 的和赋值给 c。test1 和 test2 等价,区别在于 test1 使用的 lambda 表达式是匿名的。

#include <functional>
#include <iostream>

using std::cout;
using std::endl;

int add(const int &a, const int &b) { return a + b; }

class Example {
public:
    void test1() {
        [this]() -> void {
            c = add(a, b);
        }();
    }

    void test2() {
        std::function<void()> fac = [this]() -> void {
            c = add(a, b);
        };
        fac();
    }

    int get_c() {
        test2();
        return c;
    }

private:
    int a = 1;
    int b = 2;
    int c = 0;
};

int main() {
    Example ex;
    cout << ex.get_c() << endl;
    return 0;
}

  python 中的 lambda 表达式:借助外部函数 add 把类 Example 的对象的两个数据成员 a 和 b 放入 c。

def add(a, b, c):
    c.append(a)
    c.append(b)

class Example:
    def __init__(self):
        self.a = 1
        self.b = 2
        self.c = list()

    def get_c(self):
        (lambda a, b, c: add(a, b, c))(self.a, self.b, self.c)
        return self.c

if __name__ == '__main__':
    ex = Example()
    print(ex.get_c())

5. 可调用对象与 function


  可调用对象有5种:函数、函数指针、lambda表达式、bind创建的对象、重载了函数调用运算符的类(又叫函数对象、仿函数)。
  调用形式:调用形式指明调用返回的类型以及传递给调用的实参类型。
  使用function模板类型可以把相同调用形式的对象放在一起,比如:

map<string, function<int(int, int)>>binops = {
	{"+", add}, // 函数指针。
	{"-", std::minus<int>()}, // 标准库函数对象。
	{"/", divid()}, // 用户定义的函数对象。
	{"*", [](int i, int j) {return i * j; }}, // 未命名的lambda。
	{"%", mod} }; // 命了名的lambda对象。

  这个map对象有5个元素,值是function类型。其中:

int add(int i, int j) { return i + j; }
struct divid { int operator()(int i, int j) { return i / j; } };
auto mod = [](int i, int j) {return j % j; };

  使用其它可调用对象创建function的例子:

// 1.定义普通函数、普通函数指针、普通函数引用、静态成员函数、非静态成员函数、lambda表达式、函数对象(仿函数)。
double f1(double x, int y) { return x / y; }
double (*pf1)(double, int) = f1;
double(&rf1)(double, int) = f1;
class Example {
public:
	static double f2(int x) { return x - 1.5; }
	double f3(int a, int b) { return a * b * 1.0; }
	int operator()(int x) { return -1 * x; }
};
// 2.function做参数。
void testFunction(function<double(double, int)> fun, double a, int b) { cout << fun(a, b) << endl; }
int main() {
	// 1.定义function并初始化。
	Example ex;
	function<double(double, int)> t1 = f1; // 普通函数。
	function<double(double, int)> tp1 = pf1; // 普通函数指针。
	function<double(double, int)> tr1 = rf1; // 普通函数引用。
	function<double(int)> t2 = Example::f2; // 静态成员函数。
	function<double(Example, int, int)> t31 = &Example::f3; // 非静态成员函数。
	function<double(int, int)> t32 = std::bind(&Example::f3, ex, std::placeholders::_1, std::placeholders::_2); // 非静态成员函数。ex加不加&都行。
	function<double(double, int)> t4 = [](double x, int y) {return x / y; }; // lambda表达式。
	function<int(int)> t5 = Example(); // 函数对象(仿函数functor)。注意最后的括号内没有参数。
	// 2.使用function。
	cout << t1(30, 4) << tp1(30, 4) << tr1(30, 4) << t2(30) << t31(ex, 30, 4) << t32(30, 4) << t4(30, 4) << t5(30) << endl;
	return 0;
}

6. 参考


  1. bind用法,CSDN,2019。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值