C++ 可调用对象、function、bind
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++ 可调用对象、function、bind,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4605字,纯文字阅读大概需要7分钟。
内容图文
![C++ 可调用对象、function、bind](/upload/InfoBanner/zyjiaocheng/612/6840ed90acb944dcb30e4c4b4cca6bd0.jpg)
一、可调用对象
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. 绑定普通函数
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类模板
内容总结
以上是互联网集市为您收集整理的C++ 可调用对象、function、bind全部内容,希望文章能够帮你解决C++ 可调用对象、function、bind所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。