1 Star 0 Fork 0

godeyer / clang

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
funboj.cpp 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
godeyer 提交于 2024-01-19 14:10 . 初次提交
#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();
}
1
https://gitee.com/zzzman/clang.git
git@gitee.com:zzzman/clang.git
zzzman
clang
clang
master

搜索帮助