1 Star 0 Fork 1

llongger/Code_C++

forked from GodOuO/Code_C++ 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
37.OperatorF_relo.cpp 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
GodOuO 提交于 2023-03-15 04:07 . Train
#include<iostream>
#include<string>
using namespace std;
//函数调用运算符重载,仿函数
class MyPrint
{
private:
/* data */
public:
MyPrint(/* args */);
void operator()(string thing);
};
class MyAdd
{
private:
/* data */
public:
int operator()(int num1,int num2);
MyAdd(/* args */);
};
MyAdd::MyAdd(/* args */)
{
}
int MyAdd::operator()(int num1,int num2){ //函数调用运算符()重载 传 int*2
return num1+num2;
}
MyPrint::MyPrint(/* args */)
{
}
void MyPrint::operator()(string thing){ //函数调用运算符()重载 传 string
cout<<thing<<endl;
}
void MyPrint02(string thing){
cout<<thing<<endl;
}
void test01(){
MyPrint p;
p("Hello word"); //重载 ( ) 仿函数
MyPrint02("Hello word"); //函数调用
}
void test02(){
MyAdd add;
int ret = add(100,100);
cout<<"Result: "<<ret<<endl;
}
void test03(){
cout<<"Result: "<<MyAdd()(100,200)<<endl; //匿名 函数对象(仿函数)
}
int main(){
//test01();
//test02();
test03();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/llongger/codeCpp.git
git@gitee.com:llongger/codeCpp.git
llongger
codeCpp
Code_C++
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385