当前位置: 首页 > news >正文

C++超详细讲解操作符的重载

一、需要解决的问题

下面的复数解决方案是否可行?

下面看一下复数的加法操作:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

#include <stdio.h>

classComplex

{

inta;

intb;

public:

Complex(inta = 0,intb = 0)

{

this->a = a;

this->b = b;

}

intgetA()

{

returna;

}

intgetB()

{

returnb;

}

friendComplex Add(constComplex& p1,constComplex& p2);

};

Complex Add(constComplex& p1,constComplex& p2)

{

Complex ret;

ret.a = p1.a + p2.a;

ret.b = p1.b + p2.b;

returnret;

}

intmain()

{

Complex c1(1, 2);

Complex c2(3, 4);

Complex c3 = Add(c1, c2);// c1 + c2

printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());

return0;

}

输出结果如下:

思考

Add 函数可以解决 Complex 对象相加的问题,但是 Complex 是现实世界中确实存在的复数,并且复数在数学中的地位和普通的实数相同。

为什么不能让+操作符也支持复数相加呢?这个就涉及到操作符的重载。

二、操作符重载

C++ 中的重载能够扩展操作符的功能

操作符的重载以函数的方式进行

本质

用特殊形式的函数扩展操作符的功能

  • 通过 operator 关键字可以定义特殊的函数
  • operator 的本质是通过函数重载操作符

语法

下面来初探一下操作符重载:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

#include <stdio.h>

classComplex

{

inta;

intb;

public:

Complex(inta = 0,intb = 0)

{

this->a = a;

this->b = b;

}

intgetA()

{

returna;

}

intgetB()

{

returnb;

}

friendComplex operator + (constComplex& p1,constComplex& p2);

};

Complex operator + (constComplex& p1,constComplex& p2)

{

Complex ret;

ret.a = p1.a + p2.a;

ret.b = p1.b + p2.b;

returnret;

}

intmain()

{

Complex c1(1, 2);

Complex c2(3, 4);

Complex c3 = c1 + c2;// operator + (c1, c2)

printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());

return0;

}

输出结果如下:

可以将操作符重载函数定义为类的成员函数

  • 比全局操作符重载函数少—个参数(左操作数)
  • 不需要依赖友元就可以完成操作符重载
  • 编译器优先在成员函数中寻找操作符重载函数

下面来实现在成员函数中重载操作符:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

#include <stdio.h>

classComplex

{

inta;

intb;

public:

Complex(inta = 0,intb = 0)

{

this->a = a;

this->b = b;

}

intgetA()

{

returna;

}

intgetB()

{

returnb;

}

Complex operator + (constComplex& p)

{

Complex ret;

printf("Complex operator + (const Complex& p)\n");

ret.a =this->a + p.a;

ret.b =this->b + p.b;

returnret;

}

friendComplex operator + (constComplex& p1,constComplex& p2);

};

Complex operator + (constComplex& p1,constComplex& p2)

{

Complex ret;

printf("Complex operator + (const Complex& p1, const Complex& p2)\n");

ret.a = p1.a + p2.a;

ret.b = p1.b + p2.b;

returnret;

}

intmain()

{

Complex c1(1, 2);

Complex c2(3, 4);

Complex c3 = c1 + c2;// c1.operator + (c2)

printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());

return0;

}

输出结果如下:

这个说明编译器优先在成员函数中寻找操作符重载函数

故上述代码可以直接写成:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

#include <stdio.h>

classComplex

{

inta;

intb;

public:

Complex(inta = 0,intb = 0)

{

this->a = a;

this->b = b;

}

intgetA()

{

returna;

}

intgetB()

{

returnb;

}

Complex operator + (constComplex& p)

{

Complex ret;

ret.a =this->a + p.a;

ret.b =this->b + p.b;

returnret;

}

};

intmain()

{

Complex c1(1, 2);

Complex c2(3, 4);

Complex c3 = c1 + c2;// c1.operator + (c2)

printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());

return0;

}

三、小结

  • 操作符重载是 C++ 的强大特性之一
  • 操作符重载的本质是通过函数扩展操作符的功能
  • operator 关键字是实现操作符重载的关键
  • 操作符重载遵循相同的函数重载规则
  • 全局函数和成员函数都可以实现对操作符的重载
http://www.jsqmd.com/news/708638/

相关文章:

  • 如何高效使用AB Download Manager:提升下载速度的完整指南
  • 苏州本土家装企业排行:大显空间设计领衔品质阵营 - 资讯焦点
  • 3.1《酒魂》数据设计文档(酒·药·诗)
  • LangGraph 持久化完全指南:从零搭建永不丢失状态的 AI Agent 系统
  • RPC请求服务方反序列化时,将Date类型字段转成了机器时间
  • 分析防草布源头厂家排名,德州源航新材料口碑怎么样? - 工业设备
  • 杭州GEO优化品牌推荐 - GrowthUME
  • 告别迷茫!手把手教你用STVD+COSMIC搞定STM8S105K4的第一个C语言工程
  • Flux2-Klein-9B-True-V2与物联网结合:为智能家居生成个性化场景壁纸
  • 苏州本土家装企业排行:大显空间设计领衔品质梯队 - 资讯焦点
  • 解读2026年不污染水质的养殖防渗膜,德州源航新材料口碑如何 - 工业设备
  • 别再死记硬背了!用Wireshark抓包实战,5分钟搞懂VoLTE的SIP信令到底在聊啥
  • 使用VS Code(Trae)在WSL中进行有AI智能体辅助的嵌入式Linux开发
  • 2026年国内热式气体质量流量计厂家推荐榜单:五大品牌选型指南 - 资讯焦点
  • EPANET终极指南:5步快速掌握水分配系统分析工具
  • 杭州GEO优化软件公司哪家好 - GrowthUME
  • 深度学习NLP入门:从环境搭建到模型实战
  • Dell笔记本风扇噪音终极解决方案:免费工具快速实现智能散热控制
  • 多GPU并行LLM推理能耗优化与PIE-P框架解析
  • 分析能长期合作的水泥毯供应商,哪家性价比高? - 工业设备
  • 构建有“人设”的AI助手:从LLM角色扮演到长期记忆系统实现
  • 杭州力果公司geo优化软件怎么样? - GrowthUME
  • 如何快速掌握免费开源3D重建:Meshroom从入门到精通的完整指南
  • 文墨共鸣惊艳效果:语义相似度热力图叠加水墨山水背景的可视化创新
  • 魔兽争霸III终极优化指南:用WarcraftHelper插件解锁300帧丝滑体验
  • Keras图像增强实战:提升深度学习模型性能的关键技术
  • Flux2-Klein-9B-True-V2创意工坊:结合JavaScript实现实时交互式画布生成
  • 2026年腾宇联创工业级无线网桥口碑如何,真实案例来证明 - 工业设备
  • 鞍山黄金回收门店权威排行榜TOP1:​金坊珠宝同城上门回收(24小时热线) - GrowthUME
  • Windows上安装安卓应用:APK安装器的全新体验