C++运算符重载(+)

对于内置数据类型,编译器知道如何进行运算

int a= 10:
int b=10;
int c= a + b;
class Person
I
public:
int m A:
int m_B;
Person pi;
p1.m_A = 10:
pl.m_B = 10:
Person p2;
p2.m_A = 10:
p2.m_B = 10:
Person p3=p+p2:


通过自己写成员的数,实现两个对象相加属性后返回新的对象

Person PersonAddPerson(Person 8p)
Person temp;
temp.mA= this>m_Ã +p.m A
temp.m B=this->m B + p.m B
return temp;


编译器给起了一个通用名称
通过成员函数重载+号

operator+
(Person &p)
Person
Person temp;
temp.m A=this->m A + p.m A:
temp.m B=this->m B+p.m B;
return temp;
Person p3=pi.operator+(p2):


 

简化为
Ferson p3=p1 +p2:
通过全局函数重载+
Person operator+(Person &pl,Person &p2)
Person temp;
temp,m_Ã=pl.m_À +p2.m_A;
temp.mB=pl.mB+p2.m B;
return temp;
Person p3=operator+ (pl,p2)
简化为
Person p3=p+p2:

相关推荐

  1. C++运算符重载

    2024-07-18 16:36:06       37 阅读
  2. C++:运算符重载

    2024-07-18 16:36:06       54 阅读
  3. C++ 运算符重载

    2024-07-18 16:36:06       40 阅读
  4. C++基础——运算符重载

    2024-07-18 16:36:06       40 阅读
  5. C++运算符重载

    2024-07-18 16:36:06       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 16:36:06       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 16:36:06       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 16:36:06       57 阅读
  4. Python语言-面向对象

    2024-07-18 16:36:06       68 阅读

热门阅读

  1. 使用Dockerfile构建镜像

    2024-07-18 16:36:06       20 阅读
  2. python开发基础——day14 模块与包

    2024-07-18 16:36:06       23 阅读
  3. 【国内当前可用pip&conda源刷新】

    2024-07-18 16:36:06       19 阅读
  4. 解决浏览器缓存导致获取不到最新前端代码问题

    2024-07-18 16:36:06       21 阅读
  5. 6Python的Pandas:数据读取与输出

    2024-07-18 16:36:06       22 阅读
  6. linux修改时区为CST

    2024-07-18 16:36:06       17 阅读
  7. 请求通过Spring Cloud Gateway 503

    2024-07-18 16:36:06       18 阅读
  8. 使用小皮面版的Nginx服务搭建本地服务器

    2024-07-18 16:36:06       21 阅读
  9. Jenkins 安装、部署与配置

    2024-07-18 16:36:06       23 阅读
  10. Nginx:502 Bad Gateway 错误的原因及解决方法

    2024-07-18 16:36:06       18 阅读
  11. Web开发-LinuxGit基础5-本地-分支

    2024-07-18 16:36:06       20 阅读
  12. 交易-软件科技股F4(kafka、NET、snow、MongoDB)

    2024-07-18 16:36:06       20 阅读
  13. 关于Hadoop生态系统一部分的Catalog

    2024-07-18 16:36:06       24 阅读
  14. 高内聚,低耦合的解释及实例【包学包会】

    2024-07-18 16:36:06       20 阅读