1 #include <iostream> // 标准的输入输出
2 using namespace std;
3
4 class Person {
5 public:
6 void operator()(string text)
7 {
8 ¦ cout << text << endl;
9 }
10 };
11
12 void test01()
13 {
14 Person p1;
15 p1("Hello World"); // 仿函数
16 Person()("1"); // 匿名函数
17 }
18
19 class myAdd {
20 public:
21 int operator()(int a, int b)
22 {
23 ¦ return a + b;
24 }
25 };
26
27
28 void test02()
29 {
30 ¦ cout << myAdd()(2,3) << endl;
31
32 }
33 int main()
34 {
35 test01();
36 test02();
37 return 0;
38 }
c++ 函数调用运算符重载
最新推荐文章于 2023-03-03 03:49:13 发布