C++ //函数调用运算符重载 (仿函数)
2024-09-08 01:40:51
1 //函数调用运算符重载
2
3 #include <iostream>
4 #include <string>
5 using namespace std;
6
7 //函数调用运算符重载
8
9 class MyPrint
10 {
11 public:
12
13 //重载函数调用运算符
14 void operator()(string test)
15 {
16 cout << test << endl;
17 }
18
19 };
20
21 void MyPrint02(string test)
22 {
23 cout << test << endl;
24 }
25
26 void test01()
27 {
28 MyPrint myprint;
29
30 myprint("biubiubi");//由于使用起来像函数调用 因此称为 仿函数
31
32 MyPrint02("nssidissd");
33 }
34 //仿函数非常灵活 没有固定写法
35 //加法类
36
37 class MyAdd
38 {
39
40 public:
41 int operator()(int num1, int num2)
42 {
43 return num1 + num2;
44 }
45 };
46
47
48 void test02()
49 {
50 MyAdd myadd;
51 int ret = myadd(100, 100);
52 cout << "ret = " << ret << endl;
53
54 //匿名函数对象 当前行执行完了 立即被释放
55 cout << MyAdd()(100, 100) << endl;
56 }
57 int main()
58 {
59 test01();
60 test02();
61 }
最新文章
- Jenkins FTP 上传
- 死锁及oracle死锁--转载
- Linux网络
- MessageFormat类别:快速格式化字符串
- JVM-6.即时编译器
- 书籍--嵌入式Linux驱动开发
- Java 枚举(enum) 详解7种常见的用法
- ajax请求导致status为canceled(无任何回调数据)的原因
- mysql,utf8,utf8mb4
- 一文读懂什么是Java中的自动拆装箱
- 20181016-4 Alpha阶段第1周/共2周 Scrum立会报告+燃尽图 04
- ffmpeg源码分析五:ffmpeg调用x264编码器的过程分析 (转5)
- ConstraintLayout导读
- python编码问题 与 代码换行问题
- 没有局域网环境,全是公网IP可以做LVS吗,该如何做了!请大家赐教!
- delphi小知识 点(if条件符,to_date)
- hdu1316
- Trie树,又称单词查找树、字典
- redis主从+哨兵模式
- perl中foreach(二)
热门文章
- 【原创】Ingress-Nginx-Controller的Metrics监控源码改造简析
- 13、windows下卸载oracle
- 前端 | Vue 路由返回恢复页面状态
- shell 调用其他shell脚本中的变量、函数
- php漏洞 strcmp漏洞
- Leetcode No.35 Search Insert Position(c++实现)
- ESP32的Flash加密知识
- 深入浅出 Jest 框架的实现原理
- c语言字符串存储方式
- Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)