C++ 可调用对象、function、bind

一、可调用对象

1. 函数(函数指针)

void Func(int a)
{
	cout << "a" << endl;
}

int main(int argc, const char* argv[]) 
{
	void (*p)(int) = Func;	//定义函数指针,并赋初值
	p(15);

	return 0;
}

2. 具有operator()成员函数的类对象(仿函数)

仿函数定义:行为类似于函数的东西,C++中通过在类中重载()运算符实现。又称函数对象:能行驶函数功能的类。

class Test
{
public:
	void operator()(int a)
	{
		cout << a << endl;
	}
};
int main(int argc, const char* argv[]) 
{
	Test test;
	test(15);	//调用时()运算符,也是一个可调用对象
	//test.operator(15);

	return 0;
}

3. 可被转换为函数指针的类对象(仿函数/函数对象)

class Test
{
public:
	using T = void (*)(int);	//定义函数指针类型
	static void Func(int a)	//静态成员函数
	{
		cout << a << endl;
	}
	operator T() { return Func; }

};
int main(int argc, const char* argv[]) 
{
	Test test;
	test(15);	//先调用T,在调用 Func,也是一个可调用对象

	return 0;
}

4. 类成员函数指针

class Test
{
public:
	void operator()(int a)
	{
		cout << a << endl;
	}
	void Func(int a)
	{
		cout << a << endl;
	}

	int m_a;
};
int main(int argc, const char* argv[]) 
{
	Test test;
	void (Test::*p)(int) = &Test::Func;	//类的成员函数指针变量定义并给初值
	(test.*p)(15);	//也是一个可调用对象

	return 0;
}

如何能把各种不同的可调用对象的形式统一一下?,统一的目的是方便咱们调用

二、std::function(可调用对象包装器)

包含头文件:

#include <functional>

C++11才有的,是个;类模板,用来装各种可调用对象,不能装类成员函数指针。

std::function类模板特点:就是能够给它指定模板参数,它就能够用统一的方式来处理函数。

1. 绑定普通函数

void Func(int a)
{
	cout << a << endl;
}
int main(int argc, const char* argv[]) 
{
	std::function<void(int)> F = Func;
	F(15);

	return 0;
}

2. 绑定类的静态成员函数

class Test
{
public:
	static int Func(int a)
	{
		return a;
	}
};
int main(int argc, const char* argv[]) 
{
	std::function<int(int)> F = Test::Func;	//绑定一个类的静态成员函数
	cout << F(100) << endl;

	return 0;
}

3. 绑定仿函数

class Test
{
public:
	void operator()(int a)
	{
		cout << a << endl;
	}

	int m_a = 10;
};

int main(int argc, const char* argv[]) 
{
	Test test;
	std::function<void(int)> F = test;	
	F(120);

	return 0;
}

示例代码1

class Test
{
public:
	Test(const std::function<void ()> &f): CallBack(f)
	{
		int i = 1;
	}
	void Func()
	{
		CallBack();
	}

private:
	std::function<void()> CallBack;
};

class Test2
{
public:
	void operator()(void)
	{
		cout << "operator()(void):被执行" << endl;
	}
};

int main(int argc, const char* argv[]) 
{
	Test2 test2;	//可调用对象
	Test test(test2);
	test.Func();

	return 0;
}

代码示例2

void CallBack(int i, const std::function<void(int)> &f)
{
	f(i);
}

void Func(int i)
{
	cout << i << endl;
}

int main(int argc, const char* argv[]) 
{
	for (int i = 0; i < 10; ++i)
	{
		CallBack(i, Func);
	}

	return 0;
}

三、bind绑定器

bind是一个类模板,C++11才引入,能够将对象以及相关参数绑定到一起,绑定完后可以直接调用,也可以用std::function进行保存,在需要的时候调用。

格式

std::bind(待绑定的对象/函数指针/成员函数指针, 参数绑定值1, 参数绑定值2, 参数绑定值3...);

作用

  1. 将可调用对象和参数绑定到一起,构成一个仿函数 ,所以可以直接调用。
  2. 如果函数有多个参数,可以绑定一部分参数,其他参数在调用的时候指定。

1. 绑定普通函数

void Func(int a, int b, int c)
{
	cout << "a = " << a << " ,b = " << b << " ,c = " << c << endl;
}
int main(int argc, const char* argv[]) 
{
	auto f = bind(Func, 10, 20, 30);	//返回一个仿函数类型对象,可以直接调用
	f();	//执行Func函数

	auto f1 = bind(Func, std::placeholders::_1, std::placeholders::_2, 50);
	f1(21, 22);

	return 0;
}

注意:
1. bind对于预先绑定的函数参数是通过值传递的。
2. 对于不事先绑定的参数,通过std::placeholders传递的参数,是通过引用传递的。

void Func(int& a, int& b)
{
	++a;
	++b;
}

int main(int argc, const char* argv[]) 
{
	int a = 2;
	int b = 3;
	auto f = bind(Func, a, std::placeholders::_1);
	f(b);	//执行Func函数

	cout << "a = " << a << ", b = " << b << endl;	//a = 2 , b = 4
	return 0;
}

2. 绑定成员函数

class Test
{
public:
	void Func(int x, int y)
	{
		cout << "x = " << x << ", y = " << y << endl;
		m_a = x;
	}

private:
	int m_a = 0;
};
int main(int argc, const char* argv[]) 
{
	Test test;
	auto f = bind(&Test::Func, test, std::placeholders::_1, std::placeholders::_2);
	//上行第二个参数test,会导致调用Test的拷贝构造函数来生成了一个Test类型的零时对象,作为std::bind的返回值(bind返回仿函数类型对象)
	f(10, 20);	//test成员变量的值不变

	auto f1 = bind(&Test::Func, &test, std::placeholders::_1, std::placeholders::_2);
	//上行第二个参数test前面如果加了&,就不会生成零时的Test对象了,返回对象时test对象本身
	f1(10, 20);	//test成员变量的值发生改变
			
	return 0;
}

3. std::bind和std::function配合使用

std::function<void(int, int)> f1 = bind(&Test::Func, &test, std::placeholders::_1, std::placeholders::_2);
//上行第二个参数test前面如果加了&,就不会生成零时的Test对象了,返回对象时test对象本身
f1(10, 20);	//test成员变量的值发生改变

参考博客:C++bind()函数

参考博客:C++ function类模板

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值