網站首頁 編程語言 正文
一、需要解決的問題
下面的復數解決方案是否可行?
下面看一下復數的加法操作:
#include <stdio.h>
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
friend Complex Add(const Complex& p1, const Complex& p2);
};
Complex Add(const Complex& p1, const Complex& p2)
{
Complex ret;
ret.a = p1.a + p2.a;
ret.b = p1.b + p2.b;
return ret;
}
int main()
{
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());
return 0;
}
輸出結果如下:
思考
Add 函數可以解決 Complex 對象相加的問題,但是 Complex 是現實世界中確實存在的復數,并且復數在數學中的地位和普通的實數相同。
為什么不能讓+操作符也支持復數相加呢?這個就涉及到操作符的重載。
二、操作符重載
C++ 中的重載能夠擴展操作符的功能
操作符的重載以函數的方式進行
本質
用特殊形式的函數擴展操作符的功能
- 通過 operator 關鍵字可以定義特殊的函數
- operator 的本質是通過函數重載操作符
語法
下面來初探一下操作符重載:
#include <stdio.h>
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
friend Complex operator + (const Complex& p1, const Complex& p2);
};
Complex operator + (const Complex& p1, const Complex& p2)
{
Complex ret;
ret.a = p1.a + p2.a;
ret.b = p1.b + p2.b;
return ret;
}
int main()
{
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());
return 0;
}
輸出結果如下:
可以將操作符重載函數定義為類的成員函數
- 比全局操作符重載函數少—個參數(左操作數)
- 不需要依賴友元就可以完成操作符重載
- 編譯器優先在成員函數中尋找操作符重載函數
下面來實現在成員函數中重載操作符:
#include <stdio.h>
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
Complex operator + (const Complex& p)
{
Complex ret;
printf("Complex operator + (const Complex& p)\n");
ret.a = this->a + p.a;
ret.b = this->b + p.b;
return ret;
}
friend Complex operator + (const Complex& p1, const Complex& p2);
};
Complex operator + (const Complex& p1, const Complex& 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;
return ret;
}
int main()
{
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());
return 0;
}
輸出結果如下:
這個說明編譯器優先在成員函數中尋找操作符重載函數
故上述代碼可以直接寫成:
#include <stdio.h>
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
Complex operator + (const Complex& p)
{
Complex ret;
ret.a = this->a + p.a;
ret.b = this->b + p.b;
return ret;
}
};
int main()
{
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());
return 0;
}
三、小結
- 操作符重載是 C++ 的強大特性之一
- 操作符重載的本質是通過函數擴展操作符的功能
- operator 關鍵字是實現操作符重載的關鍵
- 操作符重載遵循相同的函數重載規則
- 全局函數和成員函數都可以實現對操作符的重載
原文鏈接:https://blog.csdn.net/weixin_43129713/article/details/124362670
相關推薦
- 2022-07-03 詳解Flutter中Dart集合使用教程_Android
- 2022-12-11 Django切換數據庫和遷移數據詳解_python
- 2022-09-03 C++中std::conditional的使用說明_C 語言
- 2022-04-11 github上傳代碼error: src refspec master does not match
- 2023-11-22 Linux下設置環境變量
- 2024-07-13 Springboot使用注解實現權限校驗
- 2022-11-01 Flaks基礎之在URL中添加變量的實現詳解_python
- 2023-02-23 GoLang的sync.WaitGroup與sync.Once簡單使用講解_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支