函数对象
来源:互联网 发布:我国的医疗数据网站 编辑:程序博客网 时间:2024/05/19 20:43
重载函数调用操作符的类,其对象称为函数对象,它是行为类似函数的对象,也称作仿函数。
函数对象的特性:
1.函数对象是一个类,不是一个函数。
2.函数对象重载了()操作符,使得它可以像函数一样调用。
struct MyStruct
{
void operator()(int a)
{
cout << a << endl;
}
};
void test0()
{
MyStruct mystruct;
mystruct(1);//重载了()操作符的类实例化的对象,可以像普通函数那样调用,可以有参数,也可以由返回值
}
//函数对象超出了普通函数的概念,可以保存函数的调用状态
struct MyRcord
{
int count;
MyRcord()
{
count = 0;
}
void operator()(int a)
{
count++;
cout << a << endl;
}
};
void test1()
{
MyRcord myrcord;
myrcord(1);
myrcord(1);
myrcord(1);
myrcord(1);
cout << myrcord.count << endl;//count=4;
}
//函数对象可以做参数和返回值
struct MyReturn
{
void operator()(int a)
{
cout << a << endl;
}
};
void fun(MyReturn myreturn)
{
myreturn(10);
}
void test2()
{
MyReturn myreturn;
fun(myreturn);
}
阅读全文
0 0
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 对象 函数
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象~
- 函数对象
- juqery ajax 请求时传递数组参数
- 登陆注册案例——IO流实现
- xcode调试技巧大全--exception breakpoint, symbolic breakpoint
- LeetCode刷题(C++)——Linked List Cycle(Easy)
- CRC16检验
- 函数对象
- 【编程题】字符串分类
- mac 下 激活 phpstorm 装php
- CF
- 【开班啦】机器学习精英40天成长计划
- Java单例模式和关键字
- JavaScript DOM编程艺术第十章
- ROS学习记录(2)--控制Kobuki
- 实现多级下拉框联动获取数据