代码拉取完成,页面将自动刷新
#include<iostream>
#include<functional>
using namespace std;
class myhanshu
{
public:
myhanshu()
{
this->count = 0;
}
int operator()(int v1,int v2)
{
return v1 + v2;
}
void operator()(string name)
{
cout << name<<endl;
count++;
}
int count;
};
void dowork(myhanshu &d, string words)
{
d(words);
}
void test01()
{
//创建一个函数对象
myhanshu MYadd;
//1.函数对象在使用的时候可以像普通的函数那样调用,可以有自己的参数,可以有返回值
cout<<MYadd(3, 4)<<endl;
//函数对象超出普通函数的概念,可以有自己的状态,例如我们可以通过内置成员count统计
// myprintf调用次数。
myhanshu myprintf;
myprintf("hello world");
myprintf("hello world");
myprintf("hello world");
myprintf("hello world");
cout<<"myprintf函数一共调用了" << myprintf.count<<"次" << endl;
//类可以作为参数传递
dowork(myprintf, "wo ai c++");
cout<<"myprintf函数一共调用了" << myprintf.count<<"次" << endl;
}
int main()
{
test01();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。