当前位置:首页 > 编程笔记 > 正文
已解决

C++函数调用运算符重载

来自网友在路上 184884提问 提问时间:2023-11-19 11:26:58阅读次数: 84

最佳答案 问答题库848位专家为你答疑解惑

  1. 函数调用运算符重载()也可以重载
  2. 由于重载后使用的方式非常像函数的调用,因此称为仿函数
  3. 仿函数没有固定写法,非常灵活
#include<iostream>
using namespace std;class Myprint {
public:void operator()(string text){cout << text << endl;}
};
void test01()
{//重载的()操作符也称为仿函数Myprint myFunc;myFunc("hello world");
}
class Myadd
{
public:int operator()(int v1, int v2){return v1 + v2;}
};
void test02()
{Myadd add;int ret = add(10, 10);cout << "ret = " << ret << endl;//匿名对象调用cout << "Myadd()(100,100)=" << Myadd()(100, 100) << endl;
}int main()
{test02();test01();return 0;
}
查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"C++函数调用运算符重载":http://eshow365.cn/6-39215-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!